gpt4 book ai didi

c# - WPF 用户控件库中的 UserControl 绑定(bind)不适用于 ContentControl

转载 作者:行者123 更新时间:2023-12-03 11:03:06 25 4
gpt4 key购买 nike

为什么要绑定(bind) Caliburn.Micro UserControl包含在 WPF 用户控件库中无法使用 ContentControl ?

最初的

创建新的 Wpf App v4.6.2

  • 安装 Nuget Caliburn.Micro v3.1.0
  • 安装 Nuget Caliburn.Micro.Start v3.1.0
  • 做类似解释的改编http://caliburnmicro.com/documentation/nuget
  • 调整 app.xaml
  • 删除 StartupUri
  • 添加资源字典
  • 检查 AppBootstrapper
  • 删除 MainWindow.xaml
  • 添加按钮 x:Name="DoIt"到 ShellView.xaml
  • 添加 public void DoIt()使用 MessageBox.Show() 到 ShellViewModel.cs
  • 测试这个初始版本

  • ✓ 检查!这运行和绑定(bind)工作......

    用户控制 View
  • 添加 UserControl并将其命名为例如测试UcView
  • 添加 Textbox并给出一个名字,例如UcValue

  • <UserControl x:Class="WpfApp1.Test2UcView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    d:DesignHeight="50" d:DesignWidth="200" Visibility="{Binding UcVisibility}">
    <Grid >
    <TextBox x:Name="UcValue" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="100"/>
    <Button x:Name="UcAction" Content="Do specific" HorizontalAlignment="Left" Margin="120,10,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
    </UserControl>

    用户控制 View 模型
  • 添加对应名称的类 TestUcViewModel
  • 将类更改为公共(public)并从屏幕派生并使用 Caliburn.Micro
  • 添加
  • 将对应名称的属性添加到TextBox例如UcValue

  • public class Test1UcViewModel : Screen
    {
    private string _UcValue;
    public string UcValue { get => _UcValue; set { _UcValue = value; NotifyOfPropertyChange(() => UcValue); } }

    public void TestBut2() {
    MessageBox.Show("TestBut2");
    }
    }

    用户控制集成
  • 在 ShellViewModel 中为 ViewModel 创建公共(public)属性并在构造函数中创建一个实例
  • 在要绑定(bind)的 View 中创建控件
  • 可能性:地点ContentControlShellView.xaml与 ViewModel 属性同名(ViewModel 优先)
  • 可能性:编译并放置UserControl进入ShellView.xaml并添加 cal:Bind.Model="{Binding <ViewModel-Property-Name>}"为此命名空间

  • ✓ 检查!这运行和 UserControl 的绑定(bind)工作......

    但,

    ...现在集成第三个 UserControl属于 WPF 用户控件库 (dll),Caliburn 绑定(bind)不适用于 UserControl使用 ContentControl 的语法时来自 dll .

    public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell
    {
    private Test1UcViewModel _Test1UserControlModel;
    private Test2UcViewModel _Test2UserControlModel;
    private Test3UcViewModel _Test3UserControlModel;

    public Test1UcViewModel Test1 { get => _Test1UserControlModel; set { _Test1UserControlModel = value; NotifyOfPropertyChange(() => Test1); } }
    public Test2UcViewModel Test2 { get => _Test2UserControlModel; set { _Test2UserControlModel = value; NotifyOfPropertyChange(() => Test2); } }
    public Test3UcViewModel Test3 { get => _Test3UserControlModel; set { _Test3UserControlModel = value;NotifyOfPropertyChange(() => Test3); } }

    public ShellViewModel()
    {
    _Test1UserControlModel = new Test1UcViewModel();
    Test1.UcValue = "Bubble";
    _Test2UserControlModel = new Test2UcViewModel();
    Test2.UcValue = "Simmer";
    _Test3UserControlModel = new Test3UcViewModel();
    Test3.Uc3Value = "Knocking on heavens door";
    Test1.UcVisibility = Visibility.Visible;
    Test2.UcVisibility = Visibility.Hidden;
    Test3.UcVisibility = Visibility.Hidden;
    }

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:TestUcLib="clr-namespace:TestUcLib;assembly=TestUcLib"
    x:Class="WpfApp1.ShellView" Width="500" Height="300">

    <Grid Background="White">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100"/>
    <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <TextBlock Text="Input"
    TextWrapping="Wrap"
    VerticalAlignment="Bottom"
    HorizontalAlignment="Center"
    FontSize="20" />
    <StackPanel>
    <Button x:Name="DoIt1" Content="Do it 1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5"/>
    <Button x:Name="DoIt2" Content="Do it 2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5"/>
    <Button x:Name="DoIt3" Content="Do it 3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="5"/>
    </StackPanel>

    <ContentControl x:Name="Test1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    <ContentControl x:Name="Test2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    <ContentControl x:Name="Test3" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    <!--This one works-->
    <!--<TestUcLib:Test3UcView cal:Bind.Model="{Binding Test3}" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>-->
    </Grid>

    </Window>

    Cannot find UserControl

    是否必须使用 <TestUcLib:Test3UcView cal:Bind.Model="{Binding Test3}"当 UserControl 在 dll 中时,而不是使用 ContentControl?

    最佳答案

    Caliburn.Micro使用简单的命名模式来查找 UserControl它应该绑定(bind)到 View 模型并显示,它只搜索您通过 AssemblySource.Instance 公开为可搜索的任何程序集: http://caliburnmicro.com/documentation/conventions .

    您可以通过设置 ViewLocator.LocateForModelType 来覆盖此逻辑。属性(property)并实现您自己的。下面的基本示例应该给你的想法:

    public class HelloBootstrapper : BootstrapperBase
    {
    public HelloBootstrapper()
    {
    Initialize();
    }

    ...

    static Func<Type, DependencyObject, object, UIElement> _func;
    protected override void Configure()
    {
    var assembly = System.Reflection.Assembly.Load("WpfCustomControlLibrary1"); //<-- this is your assembly
    AssemblySource.Instance.Add(assembly);

    _func = ViewLocator.LocateForModelType;
    ViewLocator.LocateForModelType = LocateForModelType;
    ...
    }

    private static Func<Type, DependencyObject, object, UIElement> LocateForModelType = (modelType, displayLocation, context) => {

    //use the default method first:
    UIElement view = _func(modelType, displayLocation, context);
    if (!(view is TextBlock))
    return view;

    var viewTypeName = modelType.Name.Replace("Model", string.Empty);
    var viewType = (from assmebly in AssemblySource.Instance
    from type in assmebly.GetExportedTypes()
    where type.Name == viewTypeName
    select type).FirstOrDefault();

    return viewType == null ? new TextBlock { Text = string.Format("{0} not found.", viewTypeName) }
    : Activator.CreateInstance(viewType) as UIElement;
    };
    }

    关于c# - WPF 用户控件库中的 UserControl 绑定(bind)不适用于 ContentControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45883057/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com