System.AccessViolationException 在 InitializeComponent() 出现

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

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:

using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace esheets_trafalgareng
{
    public sealed partial class Tasks : Page
    {
        public static List<Button> Buttons { get; set; }
        public static Button SelectedButton;

        public Tasks()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Buttons = new List<Button>();
            foreach (Button button in secondaryContainer.Children.OfType<Button>())
            {
                if (button.Name == "myTasks") SelectedButton = button;
                Buttons.Add(button);
            }
            if (SelectedButton != null) SelectedButton.isPressed = true;
            UpdateTheme();
        }

        public void TaskListChanged(Button button)
        {
            UpdateTheme();
        }

        private void UpdateTheme()
        {
            if (Theme.IsLight()) SetLightTheme();
            else SetDarkTheme();
        }

        private void SetLightTheme()
        {
            //Theme.UseImmersiveDarkMode(this.handle, false);

            primaryContainer.Background = new SolidColorBrush(Theme.LightMain);
            primaryContainer.PaneBackground = new SolidColorBrush(Theme.LightSide);
        }

        private void SetDarkTheme()
        {
            //Theme.UseImmersiveDarkMode(this.handle, true);

            primaryContainer.Background = new SolidColorBrush(Theme.DarkMain);
            primaryContainer.PaneBackground = new SolidColorBrush(Theme.DarkSide);
        }

        internal void AddButton(Button button)
        {
            secondaryContainer.Children.Add(button);
            Buttons.Add(button);
        }
    }
}

And my XAML code:

<Page
    x:Class="esheets_trafalgareng.Tasks"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:esheets_trafalgareng"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Loaded="Page_Loaded">

    <Grid>
        <SplitView x:Name="primaryContainer" Grid.RowSpan="2" IsPaneOpen="True">
            <SplitView.Pane>
                <StackPanel x:Name="secondaryContainer">
                    <local:PersonalButton x:Name="personalButton" Margin="10,10,10,10" VerticalAlignment="Top" HorizontalAlignment="Center" Width="300" Height="54"/>
                    <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"/>
                    <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"/>
                    <local:Button x:Name="addList" Margin="0,172,0,0" VerticalAlignment="Top" Width="300" Height="36" HorizontalAlignment="Center" ImageOnly="True" IsList="False"/>
                </StackPanel>
            </SplitView.Pane>
        </SplitView>
    </Grid>
</Page>

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Windows.UI.Xaml.Controls;
using static esheets_trafalgareng.ImageOnlySettings;

namespace esheets_trafalgareng
{
    public partial class Button : Windows.UI.Xaml.Controls.Button
    {
        [Category("Appearance")]
        [Browsable(true)]
        [Localizable(true)]
        [Description("Will determine whether the button only has an image or not")]
        internal Mode ImageOnly { get; set; }

        [Category("Common")]
        [Browsable(true)]
        [Localizable(true)]
        [Description("Will determine whether the button will be used as a list or not")]
        internal Mode IsList { get; set; }

        internal readonly List<Task> tasks = new List<Task>();

        internal static int listCounter = 0;

        private static new Windows.UI.Color Background
        {
            get
            {
                if (Theme.IsLight()) return Theme.LightSide;
                else return Theme.DarkSide;
            }
        }
        private static new Windows.UI.Color Foreground
        {
            get
            {
                if (Theme.IsLight()) return Windows.UI.Color.FromArgb(0, 0, 0, 0);
                else return Windows.UI.Color.FromArgb(0, 255, 255, 255);
            }
        }
        private static readonly Windows.UI.Color LightHover = Theme.LightButtonHover;
        private static readonly Windows.UI.Color DarkHover = Theme.DarkButtonHover;

        private static readonly int FontHeight = 10;
        //private static readonly Font ButtonFont = new Font(new FontFamily("Roboto"), FontHeight, System.Drawing.FontStyle.Bold);

        private bool isHover = false;
        public bool isPressed = false;

        public Button()
        {
            AddEvents();
        }

        private void AddEvents()
        {
            PointerEntered += Button_PointerEntered;
            PointerExited += Button_PointerExited;
            PointerPressed += Button_PointerPressed;
        }

        private Tasks GetTopParent()
        {
            Control c = this;
            while (c.Parent != null)
            {
                c = (Control)c.Parent;
            }
            if (c.GetType() == typeof(Tasks)) return (Tasks)c;
            else return null;
        }

        private void Button_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if (IsList == Mode.True)
            {
                isPressed = true;
                Tasks.SelectedButton.isPressed = false;
                Tasks.SelectedButton = this;
                GetTopParent().TaskListChanged(this);
            }
            else
            {
                Button b = new Button
                {
                    Margin = Margin,
                    VerticalAlignment = VerticalAlignment,
                    Width = Width,
                    Height = Height,
                    HorizontalAlignment = HorizontalAlignment,
                    ImageOnly = Mode.False,
                    IsList = Mode.True
                };
                Margin = new Windows.UI.Xaml.Thickness(10, b.Margin.Bottom + 10, 10, 10);
                GetTopParent().AddButton(b);
            }
        }

        private void Button_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            isHover = false;
        }

        private void Button_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            isHover = true;
        }

        private Windows.UI.Color ButtonColor()
        {
            if (Theme.IsLight()) return isHover || isPressed ? LightHover : Background;
            return isHover || isPressed ? DarkHover : Background;
        }

        // OnPaint

    }

    internal class ImageOnlySettings
    {
        [Serializable]
        internal enum Mode
        {
            True,
            False
        }
    }
}
using Windows.UI;
using Windows.UI.Xaml.Input;

namespace esheets_trafalgareng
{
    public sealed class PersonalButton : Windows.UI.Xaml.Controls.Button
    {
        private static new Color Background
        {
            get
            {
                if (Theme.IsLight()) return Theme.LightSide;
                else return Theme.DarkSide;
            }
        }
        private static new Color Foreground
        {
            get
            {
                if (Theme.IsLight()) return Color.FromArgb(0, 0, 0, 0);
                else return Color.FromArgb(0, 255, 255, 255);
            }
        }
        private static Color SecondaryForeground
        {
            get
            {
                if (Theme.IsLight()) return Color.FromArgb(0, 0, 0, 0);
                else return Color.FromArgb(0, 207, 207, 207);
            }
        }
        private static readonly Color LightHover = Theme.LightButtonHover;
        private static readonly Color DarkHover = Theme.DarkButtonHover;

        /*private static new readonly int FontHeight = 10;
        private static readonly Font NameFont = new Font("Roboto", FontHeight, FontStyle.Bold);
        private static readonly Font EmailFont = new Font("Roboto", FontHeight - 1);*/

        private bool isHover = false;
        private string name;
        private string email;

        public PersonalButton()
        {
            DefaultStyleKey = typeof(PersonalButton);
            AddEvents();
        }

        private void AddEvents()
        {
            PointerEntered += OnPointerEntered;
            PointerExited += OnPointerExited;
            PointerPressed += OnPointerPressed;
        }

        private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
        {

        }

        public void OnPointerExited(object sender, PointerRoutedEventArgs e)
        {
            isHover = false;
        }

        public void OnPointerEntered(object sender, PointerRoutedEventArgs e)
        {
            isHover = true;
        }

        private Color ButtonColor()
        {
            if (Theme.IsLight()) return isHover ? LightHover : Background;
            else return isHover ? DarkHover : Background;
        }

        // OnPaint


    }
}

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

using System;
using Windows.UI;
using System.Runtime.InteropServices;
using Windows.UI.ViewManagement;

namespace esheets_trafalgareng
{
    public class Theme
    {
        internal static readonly int Light = 0;
        internal static readonly int Dark = 1;

        internal static readonly Color LightSide = Color.FromArgb(0, 208, 208, 208);
        internal static readonly Color LightTask = Color.FromArgb(0, 238, 239, 243);
        internal static readonly Color LightButtonHover = Color.FromArgb(0, 201, 201, 201);
        internal static Color LightMain
        {
            get
            {
                if (Tasks.SelectedButton.Name == "myTasks") return Color.FromArgb(0, 98, 118, 196);
                else if (Tasks.SelectedButton.Name == "assignedToMe") return Color.FromArgb(0, 213, 241, 229);
                else return Color.FromArgb(0, 153, 51, 102);
            }
        }

        internal static readonly Color DarkSide = Color.FromArgb(0, 34, 34, 34);
        internal static readonly Color DarkTask = Color.FromArgb(0, 42, 42, 42);
        internal static readonly Color DarkButtonHover = Color.FromArgb(0, 50, 50, 50);
        internal static readonly Color DarkMain = Color.FromArgb(0, 28, 28, 28);

        internal static bool IsLight()
        {
            if (Options.Theme == Light)
            {
                return true;
            }
            else if (Options.Theme == Dark)
            {
                return false;
            }
            else
            {
                if (UsingLightTheme())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        internal static bool UsingLightTheme()
        {
            UISettings DefaultTheme = new UISettings();
            string uiTheme = DefaultTheme.GetColorValue(UIColorType.Background).ToString();

            return uiTheme == "#FFFFFFFF";
        }

        [DllImport("dwmapi.dll")]
        private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
        private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;

        internal static bool UseImmersiveDarkMode(IntPtr handle, bool enabled)
        {
            if (IsWindows10OrGreater(17763))
            {
                var attribute = DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
                if (IsWindows10OrGreater(18985))
                {
                    attribute = DWMWA_USE_IMMERSIVE_DARK_MODE;
                }

                int useImmersiveDarkMode = enabled ? 1 : 0;
                return DwmSetWindowAttribute(handle, attribute, ref useImmersiveDarkMode, sizeof(int)) == 0;
            }

            return false;
        }

        private static bool IsWindows10OrGreater(int build = -1)
        {
            return Environment.OSVersion.Version.Major >= 10 && Environment.OSVersion.Version.Build >= build;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace esheets_trafalgareng
{
    internal class Options
    {
        public static int Theme { get; set; } = 3;
        public static bool IsLoggedIn { get; set; }
    }
}

答案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:

确定