如何访问类支持的资源词典实例?

发布于 2025-02-04 17:40:40 字数 1557 浏览 1 评论 0原文

我有一个资源词典,其中定义了几个控件(文本框和按钮),它们彼此无关。我可以通过tryfindresource访问控件,没有问题。

接下来,我想将事件附加到按钮上,因此我将x:class添加到字典中,并创建了一个备份类(基于我在此处找到的信息),事件正常运行。

我遇到的问题是:如何获得支持负载XAML的创建资源目录(即我的课程)?最终,我想在资源目录中创建许多复杂的控件,并具有一个简单的单个功能,我可以在备用类中调用,以便给我这些控件的列表,因此我可以将它们添加到我的窗口中,而不是需要致电为每个控件进行tryfindresource。

我在类中有一个构造函数(并且可以调用initializecomponent),但是自动创建的内部字段代表文本框和按钮为空。该构造函数被称为早期(在我的GUI中的任何内容之前),但是即使在我的窗户的加载事件被称为时,幕后连接呼叫也显然还没有完成。

我已经进行了大量的搜索,但是我发现的所有内容都停止了定义班级,此后没有显示任何用法。

我在这里吠叫错误吗?我敢肯定,我误解了所有功能以及它的能力的方式,但似乎我应该能够以某种方式获得实例。

部分XAML:

<ResourceDictionary x:Class="MM.Window.TitleBarControls"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <MMM:ApplicationInformation x:Key="ApplicationInformation" />

    <Button x:Key="ButtonTest"
            x:Name="ButtonTest"
            Content="Button"
            Click="ButtonClickTest" />
</ResourceDictionary>

部分类别:

    public partial class TitleBarControls : ResourceDictionary
    {
        public ObservableList<object> HeaderControls = new();

        public TitleBarControls()
        {
            TitleBarControlsMain = this;

            InitializeComponent();

            // ButtonTest is null here
            HeaderControls.Add(ButtonTest);
        }

        private void ButtonClickTest(object sender_, System.Windows.RoutedEventArgs e_)
        {
            int i = 1;
        }
    }

I've got a resource dictionary with several control defined in it (TextBox and Button) that are unrelated to each other. I can access the controls via TryFindResource without problems.

Next, I wanted to attach an event to the button, so I added x:Class to the dictionary and created a backing class (based on info I found here) and the event functions correctly.

What I'm having problems with is: how do I gain access to the created resource directory (i.e. my class) that backs the load xaml? Ultimately I want to create a number of complicated controls in the resource directory and have a simple, single function I can call in the backing class to give me the list of those controls back, so I can add them to my window, rather than needing call to TryFindResource for each control.

I have a constructor in the class (and have a call to InitializeComponent), but the auto created internal fields that represent the TextBox and Button are null. The constructor is called early (well before anything in my GUI) but the behind-the-scenese Connect call apparently hasn't been done even by the time my window's Loaded event is called.

I've done a ton of searching but everything I've found stops at the defining the class point and doesn't show any usage after that.

Am I barking up the wrong tree here? I'm sure I'm misunderstanding something about how this all functions and what it's capable of, but it really seems like I should be able to get an instance somehow.

Partial XAML:

<ResourceDictionary x:Class="MM.Window.TitleBarControls"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <MMM:ApplicationInformation x:Key="ApplicationInformation" />

    <Button x:Key="ButtonTest"
            x:Name="ButtonTest"
            Content="Button"
            Click="ButtonClickTest" />
</ResourceDictionary>

Partial class:

    public partial class TitleBarControls : ResourceDictionary
    {
        public ObservableList<object> HeaderControls = new();

        public TitleBarControls()
        {
            TitleBarControlsMain = this;

            InitializeComponent();

            // ButtonTest is null here
            HeaderControls.Add(ButtonTest);
        }

        private void ButtonClickTest(object sender_, System.Windows.RoutedEventArgs e_)
        {
            int i = 1;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2025-02-11 17:40:40

示例中的按钮是 Resource ,它将添加到resourcectionary本身中。

删除x:name(避免混淆)从XAML标记中删除,并使用代码范围中的索引器访问它:

HeaderControls.Add(this["ButtonTest"]);

The button in your example is a resource that gets added to the ResourceDictionary itself.

Remove the x:Name (to avoid confusion) from the XAML markup and access it using the indexer in the code-behind:

HeaderControls.Add(this["ButtonTest"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文