PRISM RegionManager - ItemsControl RegionManager 的非 XAML 创建

发布于 2024-07-29 17:13:31 字数 1373 浏览 4 评论 0原文

我对 PRISM 中的区域有一个小问题。 所有基础测试都工作正常,但现在我想用纯 C# 替换以下 XAML:

<UserControl x:Class="CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
    Height="Auto" Width="Auto">
        <ItemsControl cal:RegionManager.RegionName="ItemsControlRegionAdapterTestRegion"/>
</UserControl>

我的测试类中的代码相当简单,我访问 RegionManager 并添加一些测试视图。 然而,正如您在上面的 XAML 中看到的,除了将 RegionManager 附加到 Control 之外,UserControl 中实际上没有发生任何事情。 我确信这在代码中一定是可能的,扩展了我已经拥有的以下行:

        // MISSING
        // Creating the UserControl in CODE instead of XAML


        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, new ItemsControlRegionAdapterTest());

        // Add some views to the region inside the user control
        var currentTestRegionName = TestingRegionNames.ItemsControlRegionAdapterTestRegion;
        regionManager.Regions[currentTestRegionName].Add(new BlueView());
        regionManager.Regions[currentTestRegionName].Add(new RedView());

感谢您的任何提示...

I have a small problem with Regions in PRISM. All basics tests work fine, but now I want to replace the following XAML with pure C#:

<UserControl x:Class="CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
    Height="Auto" Width="Auto">
        <ItemsControl cal:RegionManager.RegionName="ItemsControlRegionAdapterTestRegion"/>
</UserControl>

The code inside my testing class is fairly simple, I access the RegionManager and add some test views. However, as you see in the XAML above, there is actual nothing happening in the UserControl except from attaching the RegionManager to the Control. I am sure this must be possible in Code, extending the following lines I already have:

        // MISSING
        // Creating the UserControl in CODE instead of XAML


        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, new ItemsControlRegionAdapterTest());

        // Add some views to the region inside the user control
        var currentTestRegionName = TestingRegionNames.ItemsControlRegionAdapterTestRegion;
        regionManager.Regions[currentTestRegionName].Add(new BlueView());
        regionManager.Regions[currentTestRegionName].Add(new RedView());

Thanks for any tips...

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

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

发布评论

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

评论(2

对你而言 2024-08-05 17:13:31

好的,XamlReader 方法正在工作(一些修正,请参阅随附的源代码)...

但说实话,它看起来有点难看:-)
因此,如果有人知道如何“在代码中附加区域管理器”,欢迎详细信息。
首先,有效的 XAML 读取器行是:

        // MISSING
        // Creating the UserControl in CODE instead of XAML
        var obj = (UserControl)XamlReader.Parse(@"<UserControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                                               xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
Height=""Auto"" Width=""Auto"">
    <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/></UserControl>");

        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, obj);

明白了! (至少它有效,不确定是否是最佳实践)

        var uC = new UserControl();
        var iC = new ItemsControl();
        uC.Content = iC;

        RegionManager.SetRegionName(iC, "ItemsControlRegionAdapterTestRegion");

        regionManager.AddToRegion(RegionNames.MainRegion, uC);

感谢所有帮助...仍然欢迎评论...

Ok, the XamlReader Approach is working (little corrections, see source code attached)...

But honestly, it looks a little bit ugly :-)
So if anybody knows how to "attach the regionManager in Code", details are welcome.
First, the working XAML reader lines are:

        // MISSING
        // Creating the UserControl in CODE instead of XAML
        var obj = (UserControl)XamlReader.Parse(@"<UserControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                                               xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
Height=""Auto"" Width=""Auto"">
    <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/></UserControl>");

        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, obj);

GOT IT!! (at least it is working, not sure if best practice)

        var uC = new UserControl();
        var iC = new ItemsControl();
        uC.Content = iC;

        RegionManager.SetRegionName(iC, "ItemsControlRegionAdapterTestRegion");

        regionManager.AddToRegion(RegionNames.MainRegion, uC);

Thanks for all help... Comments still welcome...

著墨染雨君画夕 2024-08-05 17:13:31

尝试 XamlReader 方法:

private const string xaml = @"
<UserControl x:Class=""CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest""
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
    Height=""Auto"" Width=""Auto"">
        <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/>
</UserControl>";

//in some method
public void RunMethod()
{
     //create object and cast it
     ItemsControlRegionAdapterTest obj = (ItemsControlRegionAdapterTest) XamlReader.Parse(xaml);
}

Try the XamlReader approach:

private const string xaml = @"
<UserControl x:Class=""CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest""
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
    Height=""Auto"" Width=""Auto"">
        <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/>
</UserControl>";

//in some method
public void RunMethod()
{
     //create object and cast it
     ItemsControlRegionAdapterTest obj = (ItemsControlRegionAdapterTest) XamlReader.Parse(xaml);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文