System.AccessViolationException 在 InitializeComponent() 出现

huangapple go评论105阅读模式
英文:

System.AccessViolationException at InitializeComponent()

问题

我注意到你提供了一些代码和XAML,但你没有提出具体的问题或翻译请求。请问你需要针对这些代码或XAML的哪部分进行翻译或需要什么样的帮助?

英文:

I'm working on a UWP app, and it keeps crashing at the InitializeComponent(); call with the error message: System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Here's my code for this class:

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Windows.UI.Xaml;
  4. using Windows.UI.Xaml.Controls;
  5. using Windows.UI.Xaml.Media;
  6. namespace esheets_trafalgareng
  7. {
  8. public sealed partial class Tasks : Page
  9. {
  10. public static List<Button> Buttons { get; set; }
  11. public static Button SelectedButton;
  12. public Tasks()
  13. {
  14. InitializeComponent();
  15. }
  16. private void Page_Loaded(object sender, RoutedEventArgs e)
  17. {
  18. Buttons = new List<Button>();
  19. foreach (Button button in secondaryContainer.Children.OfType<Button>())
  20. {
  21. if (button.Name == "myTasks") SelectedButton = button;
  22. Buttons.Add(button);
  23. }
  24. if (SelectedButton != null) SelectedButton.isPressed = true;
  25. UpdateTheme();
  26. }
  27. public void TaskListChanged(Button button)
  28. {
  29. UpdateTheme();
  30. }
  31. private void UpdateTheme()
  32. {
  33. if (Theme.IsLight()) SetLightTheme();
  34. else SetDarkTheme();
  35. }
  36. private void SetLightTheme()
  37. {
  38. //Theme.UseImmersiveDarkMode(this.handle, false);
  39. primaryContainer.Background = new SolidColorBrush(Theme.LightMain);
  40. primaryContainer.PaneBackground = new SolidColorBrush(Theme.LightSide);
  41. }
  42. private void SetDarkTheme()
  43. {
  44. //Theme.UseImmersiveDarkMode(this.handle, true);
  45. primaryContainer.Background = new SolidColorBrush(Theme.DarkMain);
  46. primaryContainer.PaneBackground = new SolidColorBrush(Theme.DarkSide);
  47. }
  48. internal void AddButton(Button button)
  49. {
  50. secondaryContainer.Children.Add(button);
  51. Buttons.Add(button);
  52. }
  53. }
  54. }

And my XAML code:

  1. <Page
  2. x:Class="esheets_trafalgareng.Tasks"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:esheets_trafalgareng"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d"
  9. Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Loaded="Page_Loaded">
  10. <Grid>
  11. <SplitView x:Name="primaryContainer" Grid.RowSpan="2" IsPaneOpen="True">
  12. <SplitView.Pane>
  13. <StackPanel x:Name="secondaryContainer">
  14. <local:PersonalButton x:Name="personalButton" Margin="10,10,10,10" VerticalAlignment="Top" HorizontalAlignment="Center" Width="300" Height="54"/>
  15. <local:Button x:Name="myTasks" Content="My Tasks" Margin="10,90,10,10" VerticalAlignment="Top" HorizontalAlignment="Center" Width="300" Height="36" ImageOnly="False" IsList="True"/>
  16. <local:Button x:Name="assignedToMe" Content="Assigned To Me" Margin="0,131,0,0" VerticalAlignment="Top" Width="300" Height="36" HorizontalAlignment="Center" ImageOnly="False" IsList="True"/>
  17. <local:Button x:Name="addList" Margin="0,172,0,0" VerticalAlignment="Top" Width="300" Height="36" HorizontalAlignment="Center" ImageOnly="True" IsList="False"/>
  18. </StackPanel>
  19. </SplitView.Pane>
  20. </SplitView>
  21. </Grid>
  22. </Page>

The Button and PersonalButton components are custom components and I've put their code below:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using Windows.UI.Xaml.Controls;
  5. using static esheets_trafalgareng.ImageOnlySettings;
  6. namespace esheets_trafalgareng
  7. {
  8. public partial class Button : Windows.UI.Xaml.Controls.Button
  9. {
  10. [Category("Appearance")]
  11. [Browsable(true)]
  12. [Localizable(true)]
  13. [Description("Will determine whether the button only has an image or not")]
  14. internal Mode ImageOnly { get; set; }
  15. [Category("Common")]
  16. [Browsable(true)]
  17. [Localizable(true)]
  18. [Description("Will determine whether the button will be used as a list or not")]
  19. internal Mode IsList { get; set; }
  20. internal readonly List<Task> tasks = new List<Task>();
  21. internal static int listCounter = 0;
  22. private static new Windows.UI.Color Background
  23. {
  24. get
  25. {
  26. if (Theme.IsLight()) return Theme.LightSide;
  27. else return Theme.DarkSide;
  28. }
  29. }
  30. private static new Windows.UI.Color Foreground
  31. {
  32. get
  33. {
  34. if (Theme.IsLight()) return Windows.UI.Color.FromArgb(0, 0, 0, 0);
  35. else return Windows.UI.Color.FromArgb(0, 255, 255, 255);
  36. }
  37. }
  38. private static readonly Windows.UI.Color LightHover = Theme.LightButtonHover;
  39. private static readonly Windows.UI.Color DarkHover = Theme.DarkButtonHover;
  40. private static readonly int FontHeight = 10;
  41. //private static readonly Font ButtonFont = new Font(new FontFamily("Roboto"), FontHeight, System.Drawing.FontStyle.Bold);
  42. private bool isHover = false;
  43. public bool isPressed = false;
  44. public Button()
  45. {
  46. AddEvents();
  47. }
  48. private void AddEvents()
  49. {
  50. PointerEntered += Button_PointerEntered;
  51. PointerExited += Button_PointerExited;
  52. PointerPressed += Button_PointerPressed;
  53. }
  54. private Tasks GetTopParent()
  55. {
  56. Control c = this;
  57. while (c.Parent != null)
  58. {
  59. c = (Control)c.Parent;
  60. }
  61. if (c.GetType() == typeof(Tasks)) return (Tasks)c;
  62. else return null;
  63. }
  64. private void Button_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
  65. {
  66. if (IsList == Mode.True)
  67. {
  68. isPressed = true;
  69. Tasks.SelectedButton.isPressed = false;
  70. Tasks.SelectedButton = this;
  71. GetTopParent().TaskListChanged(this);
  72. }
  73. else
  74. {
  75. Button b = new Button
  76. {
  77. Margin = Margin,
  78. VerticalAlignment = VerticalAlignment,
  79. Width = Width,
  80. Height = Height,
  81. HorizontalAlignment = HorizontalAlignment,
  82. ImageOnly = Mode.False,
  83. IsList = Mode.True
  84. };
  85. Margin = new Windows.UI.Xaml.Thickness(10, b.Margin.Bottom + 10, 10, 10);
  86. GetTopParent().AddButton(b);
  87. }
  88. }
  89. private void Button_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
  90. {
  91. isHover = false;
  92. }
  93. private void Button_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
  94. {
  95. isHover = true;
  96. }
  97. private Windows.UI.Color ButtonColor()
  98. {
  99. if (Theme.IsLight()) return isHover || isPressed ? LightHover : Background;
  100. return isHover || isPressed ? DarkHover : Background;
  101. }
  102. // OnPaint
  103. }
  104. internal class ImageOnlySettings
  105. {
  106. [Serializable]
  107. internal enum Mode
  108. {
  109. True,
  110. False
  111. }
  112. }
  113. }
  1. using Windows.UI;
  2. using Windows.UI.Xaml.Input;
  3. namespace esheets_trafalgareng
  4. {
  5. public sealed class PersonalButton : Windows.UI.Xaml.Controls.Button
  6. {
  7. private static new Color Background
  8. {
  9. get
  10. {
  11. if (Theme.IsLight()) return Theme.LightSide;
  12. else return Theme.DarkSide;
  13. }
  14. }
  15. private static new Color Foreground
  16. {
  17. get
  18. {
  19. if (Theme.IsLight()) return Color.FromArgb(0, 0, 0, 0);
  20. else return Color.FromArgb(0, 255, 255, 255);
  21. }
  22. }
  23. private static Color SecondaryForeground
  24. {
  25. get
  26. {
  27. if (Theme.IsLight()) return Color.FromArgb(0, 0, 0, 0);
  28. else return Color.FromArgb(0, 207, 207, 207);
  29. }
  30. }
  31. private static readonly Color LightHover = Theme.LightButtonHover;
  32. private static readonly Color DarkHover = Theme.DarkButtonHover;
  33. /*private static new readonly int FontHeight = 10;
  34. private static readonly Font NameFont = new Font("Roboto", FontHeight, FontStyle.Bold);
  35. private static readonly Font EmailFont = new Font("Roboto", FontHeight - 1);*/
  36. private bool isHover = false;
  37. private string name;
  38. private string email;
  39. public PersonalButton()
  40. {
  41. DefaultStyleKey = typeof(PersonalButton);
  42. AddEvents();
  43. }
  44. private void AddEvents()
  45. {
  46. PointerEntered += OnPointerEntered;
  47. PointerExited += OnPointerExited;
  48. PointerPressed += OnPointerPressed;
  49. }
  50. private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
  51. {
  52. }
  53. public void OnPointerExited(object sender, PointerRoutedEventArgs e)
  54. {
  55. isHover = false;
  56. }
  57. public void OnPointerEntered(object sender, PointerRoutedEventArgs e)
  58. {
  59. isHover = true;
  60. }
  61. private Color ButtonColor()
  62. {
  63. if (Theme.IsLight()) return isHover ? LightHover : Background;
  64. else return isHover ? DarkHover : Background;
  65. }
  66. // OnPaint
  67. }
  68. }

Here's 2 other classes that I use, in case they are needed:

  1. using System;
  2. using Windows.UI;
  3. using System.Runtime.InteropServices;
  4. using Windows.UI.ViewManagement;
  5. namespace esheets_trafalgareng
  6. {
  7. public class Theme
  8. {
  9. internal static readonly int Light = 0;
  10. internal static readonly int Dark = 1;
  11. internal static readonly Color LightSide = Color.FromArgb(0, 208, 208, 208);
  12. internal static readonly Color LightTask = Color.FromArgb(0, 238, 239, 243);
  13. internal static readonly Color LightButtonHover = Color.FromArgb(0, 201, 201, 201);
  14. internal static Color LightMain
  15. {
  16. get
  17. {
  18. if (Tasks.SelectedButton.Name == "myTasks") return Color.FromArgb(0, 98, 118, 196);
  19. else if (Tasks.SelectedButton.Name == "assignedToMe") return Color.FromArgb(0, 213, 241, 229);
  20. else return Color.FromArgb(0, 153, 51, 102);
  21. }
  22. }
  23. internal static readonly Color DarkSide = Color.FromArgb(0, 34, 34, 34);
  24. internal static readonly Color DarkTask = Color.FromArgb(0, 42, 42, 42);
  25. internal static readonly Color DarkButtonHover = Color.FromArgb(0, 50, 50, 50);
  26. internal static readonly Color DarkMain = Color.FromArgb(0, 28, 28, 28);
  27. internal static bool IsLight()
  28. {
  29. if (Options.Theme == Light)
  30. {
  31. return true;
  32. }
  33. else if (Options.Theme == Dark)
  34. {
  35. return false;
  36. }
  37. else
  38. {
  39. if (UsingLightTheme())
  40. {
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. }
  49. internal static bool UsingLightTheme()
  50. {
  51. UISettings DefaultTheme = new UISettings();
  52. string uiTheme = DefaultTheme.GetColorValue(UIColorType.Background).ToString();
  53. return uiTheme == "#FFFFFFFF";
  54. }
  55. [DllImport("dwmapi.dll")]
  56. private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
  57. private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
  58. private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
  59. internal static bool UseImmersiveDarkMode(IntPtr handle, bool enabled)
  60. {
  61. if (IsWindows10OrGreater(17763))
  62. {
  63. var attribute = DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
  64. if (IsWindows10OrGreater(18985))
  65. {
  66. attribute = DWMWA_USE_IMMERSIVE_DARK_MODE;
  67. }
  68. int useImmersiveDarkMode = enabled ? 1 : 0;
  69. return DwmSetWindowAttribute(handle, attribute, ref useImmersiveDarkMode, sizeof(int)) == 0;
  70. }
  71. return false;
  72. }
  73. private static bool IsWindows10OrGreater(int build = -1)
  74. {
  75. return Environment.OSVersion.Version.Major >= 10 && Environment.OSVersion.Version.Build >= build;
  76. }
  77. }
  78. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace esheets_trafalgareng
  7. {
  8. internal class Options
  9. {
  10. public static int Theme { get; set; } = 3;
  11. public static bool IsLoggedIn { get; set; }
  12. }
  13. }

答案1

得分: 0

问题是我将组件定义为C#类而不是用户控件。如果其他人遇到此问题,请确保为您的组件有一个.xaml文件和一个.cs.xaml文件!

英文:

After some research, I figured out that the problem was that I was defining my components as C# classes rather than User Controls. If anyone else comes across this issue, make sure you have a .xaml and a .cs.xaml file for your components!

huangapple
  • 本文由 发表于 2023年6月16日 01:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定