An unhandled exception of type 'System.AccessViolationException' occurred in Notepad.exe
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
我不确定为什么会发生这种情况。我的 XAML:
<Page
x:Class="Notepad.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Notepad"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:winui="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d winui"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25px"></RowDefinition>
<RowDefinition Height="25px"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="25px"></RowDefinition>
</Grid.RowDefinitions>
<MenuBar Grid.Row="0" Height="22.5px">
<MenuBarItem Title="File">
<MenuFlyoutItem Text="New">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="N">
</KeyboardAccelerator>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutItem Text="New Window" Click="New_Window_Click" Loaded="extra.LOADER_LOAD">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Key="N">
<KeyboardAccelerator.Modifiers>
Control
<!-- 5 = Control & Shift -->
</KeyboardAccelerator.Modifiers>
</KeyboardAccelerator>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutItem Text="Open...">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="O">
</KeyboardAccelerator>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutItem Text="Save">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="S">
</KeyboardAccelerator>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutItem Text="Save As..." Loaded="extra.LOADER_LOAD">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Key="S">
<KeyboardAccelerator.Modifiers>
Control<!-- 5 = Control & Shift -->
</KeyboardAccelerator.Modifiers>
</KeyboardAccelerator>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutSeparator></MenuFlyoutSeparator>
<MenuFlyoutItem Text="Page Setup"></MenuFlyoutItem>
<MenuFlyoutItem Text="Print...">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="P"></KeyboardAccelerator>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
<MenuFlyoutSeparator></MenuFlyoutSeparator>
<MenuFlyoutItem Text="Exit" Click="Exit_Click"></MenuFlyoutItem>
</MenuBarItem>
<MenuBarItem Title="Edit">
<MenuFlyoutItem Text="Undo"></MenuFlyoutItem>
<MenuFlyoutSeparator></MenuFlyoutSeparator>
<MenuFlyoutItem Text="Cut"></MenuFlyoutItem>
<MenuFlyoutItem Text="Copy"></MenuFlyoutItem>
<MenuFlyoutItem Text="Paste"></MenuFlyoutItem>
<MenuFlyoutItem Text="Delete"></MenuFlyoutItem>
<MenuFlyoutSeparator></MenuFlyoutSeparator>
<MenuFlyoutItem Text="Search"></MenuFlyoutItem>
<MenuFlyoutItem Text="Find..."></MenuFlyoutItem>
<MenuFlyoutItem Text="Find Next"></MenuFlyoutItem>
<MenuFlyoutItem Text="Find Previous"></MenuFlyoutItem>
<MenuFlyoutItem Text="Replace"></MenuFlyoutItem>
<MenuFlyoutItem Text="Go To..."></MenuFlyoutItem>
<MenuFlyoutSeparator></MenuFlyoutSeparator>
<MenuFlyoutItem Text="Select All"></MenuFlyoutItem>
<MenuFlyoutItem Text="Time/Date"></MenuFlyoutItem>
</MenuBarItem>
<MenuBarItem Title="Format">
<ToggleMenuFlyoutItem Text="Word Wrap"></ToggleMenuFlyoutItem>
<MenuFlyoutItem Text="Font..."></MenuFlyoutItem>
</MenuBarItem>
<MenuBarItem Title="View">
<MenuFlyoutSubItem Text="Zoom">
<MenuFlyoutItem Text="Zoom In"></MenuFlyoutItem>
<MenuFlyoutItem Text="Zoom Out"></MenuFlyoutItem>
<MenuFlyoutItem Text="Restore Default Zoom"></MenuFlyoutItem>
</MenuFlyoutSubItem>
<ToggleMenuFlyoutItem Text="Status Bar"></ToggleMenuFlyoutItem>
</MenuBarItem>
<MenuBarItem Title="Help">
<MenuFlyoutItem Text="View Help"></MenuFlyoutItem>
<MenuFlyoutItem Text="Send Feedback"></MenuFlyoutItem>
<MenuFlyoutSeparator></MenuFlyoutSeparator>
<MenuFlyoutItem Text="About Notepad"></MenuFlyoutItem>
</MenuBarItem>
</MenuBar>
<winui:TabView x:Name="Documents" Grid.Row="1" Loaded="Documents_Loaded" AddTabButtonClick="Documents_AddTabButtonClick" TabCloseRequested="Documents_TabCloseRequested" CanReorderTabs="True" CanDragTabs="True" SelectionChanged="Documents_SelectionChanged"></winui:TabView>
<TextBox x:Name="WritePanel" Grid.Row="2" BorderThickness="0" AcceptsReturn="True" ScrollViewer.VerticalScrollBarVisibility="Visible">
</TextBox>
</Grid>
</Page>
我的 xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.ComponentModel;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.ApplicationModel.Core;
using ViewsHelpers;
using Microsoft.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Notepad
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public Extra extra = new();
public MainPage()
{
this.InitializeComponent();
}
private async void New_Window_Click(object sender, RoutedEventArgs e)
{
ViewLifetimeControl viewControl = null;
await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// This object is used to keep track of the views and important
// details about the contents of those views across threads
// In your app, you would probably want to track information
// like the open document or page inside that window
viewControl = ViewLifetimeControl.CreateForCurrentView();
const string DEFAULT_TITLE = "";
viewControl.Title = DEFAULT_TITLE;
// Increment the ref count because we just created the view and we have a reference to it
viewControl.StartViewInUse();
var frame = new Frame();
frame.Navigate(typeof(MainPage), viewControl);
Window.Current.Content = frame;
// This is a change from 8.1: In order for the view to be displayed later it needs to be activated.
Window.Current.Activate();
ApplicationView.GetForCurrentView().Title = viewControl.Title;
});
var succeeded = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(viewControl.Id);
System.Diagnostics.Debug.Assert(succeeded);
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Exit();
}
private void Documents_Loaded(object sender, RoutedEventArgs e)
{
var xsender = sender as TabView;
var tvi = new TabViewItem();
tvi.Header = "Unnamed Document";
xsender.TabItems.Add(tvi);
}
private void Documents_AddTabButtonClick(Microsoft.UI.Xaml.Controls.TabView sender, object args)
{
var tvi = new TabViewItem();
tvi.Header = "Unnamed Document";
sender.TabItems.Add(tvi);
}
private void Documents_TabCloseRequested(Microsoft.UI.Xaml.Controls.TabView sender, Microsoft.UI.Xaml.Controls.TabViewTabCloseRequestedEventArgs args)
{
sender.TabItems.Remove(args.Tab);
}
private void Documents_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var strheader = (e.RemovedItems[0] as TabViewItem)?.Header;
if(strheader != null) strheader = WritePanel.Text;
var nextstrheader = (e.AddedItems[0] as TabViewItem)?.Header;
if (nextstrheader != null) WritePanel.Text = nextstrheader.ToString();
}
}
public sealed class Extra
{
public void LOADER_LOAD(object sender, RoutedEventArgs e)
{
var mfi = sender as MenuFlyoutItem;
mfi.KeyboardAccelerators[0].Modifiers = Windows.System.VirtualKeyModifiers.Control | Windows.System.VirtualKeyModifiers.Shift;
}
}
}
请注意,我使用的是 Microsoft.UI.Xaml
。
另外,抛出异常后,会出现这个banner: 单击“显示堆栈跟踪”按钮时,会出现:
The Solution Does Not Contain The Specified Document:
StreamJsonRpc.RemoteInvocationException: The solution does not contain the specified document.
at StreamJsonRpc.JsonRpc.<InvokeCoreAsync>d__139`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection`1.<TryInvokeAsync>d__20`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
RPC server exception:
System.InvalidOperationException: The solution does not contain the specified document.
at Microsoft.CodeAnalysis.Shared.Extensions.ISolutionExtensions.GetRequiredDocument(Solution solution, DocumentId documentId)
at Microsoft.CodeAnalysis.EditAndContinue.RemoteEditAndContinueService.<>c__DisplayClass16_0.<<GetAdjustedActiveStatementSpansAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.<RunServiceImplAsync>d__12`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)