如何在应用程序中使用框架中的视图?(WPF MVVM) [英] How can I use a view from a framework in an application? (WPF MVVM)

查看:22
本文介绍了如何在应用程序中使用框架中的视图?(WPF MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个自定义框架中创建了一个 BaseWindowView 和一个 BaseViewModel,我想将其用作我所有应用程序的基础.

I have created a BaseWindowView and a BaseViewModel in a self-defined framework which I'd like to use as a basis in all of my applications.

BaseViewModel 中,例如,我有一个方法可以添加按钮.我可以定义一个 Command、一个 ButtonImage 和一个 LabelString.下面是调用这个方法的示例代码:

In the BaseViewModel I have, for example, a method which allows me to add buttons. I can define a Command, a ButtonImage and a LabelString. Here is the sample code for the call of this method:

AddButton(OpenAnyTabCommand, "../Images/image.png", "LabelString");

在我的 BaseWindowView 中,我有一个 RibbonMenue,其中我在 BaseViewModel 中定义的所有 RibbonButtons显示:

In my BaseWindowView I have a RibbonMenue where all of the RibbonButtons that I have defined in the BaseViewModel are shown:

<ItemsControl x:Name="CButtons" ItemsSource="{Binding Buttons}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel x:Name="ButtonStackPanel" Orientation="Horizontal">
            </StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ribbon:RibbonButton
                x:Uid="Button"
                LargeImageSource="{Binding Path=ImageString}"
                Label="{Binding Path=LabelString}"
                Command="{Binding Path=ButtonCommand}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我在一个示例项目中有这个,它工作正常.现在我想将 BaseWindowViewBaseViewModel 外包给一个框架并从那里使用它.

I have this in a sample project, and it works fine. Now I want to outsource the BaseWindowView and the BaseViewModel to a framework and use it from there.

我的计划是:在我的每个应用程序中,我想将 BaseWindowView 设置为我的应用程序的 MainWindow.在这个应用程序本身中,我只想有一些 UserControls,它们应该在我的 BaseWindowView 中显示为选项卡.我在 BaseViewModel 中定义的按钮应该调用定义的 Command,它会打开一个新选项卡并显示此 Command 后面的 ViewModel.

My plan is: In each of my applications, I'd like to set the BaseWindowView as the MainWindow of my application. In this application itself I just want to have some UserControls which should be shown as tabs in my BaseWindowView. The buttons I've defined in the BaseViewModel should call the defined Command which opens a new tab and shows the ViewModel behind this Command.

那么处理我的问题的最佳方法是什么?我知道 XAML 中没有经典继承".我可以做一些事情,比如将 App.xaml 中的 StartUpUri 定义到框架视图,还是有点"棘手?

So what is the best way to handle my problem? I know that there is no "classic inheritance" in XAML. Can I just do something like define the StartUpUri in the App.xaml to the framework view or is it "a little bit" trickier?

作为补充:我发现我可以在 App 中为 TabItems(它们是 UserControls)定义 DataTemplate.xaml 如下所示:

As an addition: I found that I can define the DataTemplate for the TabItems (which are UserControls) in the App.xaml like the following:

<DataTemplate DataType="{x:Type ViewModel:AnyViewModel}">
    <view:TabItemAny/>
</DataTemplate>

@Sheridan:这是关于 BaseWindowView 的问题.

@Sheridan: Here is the question about the BaseWindowView.

推荐答案

好的,首先,让我们使用 BaseWindowView 作为 MainWindow:

Ok, so to start with, let's address using your BaseWindowView as the MainWindow:

首先,在 App.xaml 文件中,从 Application 中删除 StartupStartupUri 属性的声明> 定义.然后在 App.xaml.cs 类中,添加一个启动处理程序:

First, in the App.xaml file, remove the declaration of Startup and StartupUri properties from the Application definition. Then in the App.xaml.cs class, add a startup handler:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    BaseWindowView baseWindowView = new BaseWindowView();
    baseWindowView.Show();
}

这将打开您的 Window 而不是 MainWindow.

This will open your Window instead of the MainWindow.

现在来看视图继承……如您所知,WPF 中没有这样的东西,但是有(某种)解决这个问题的方法.如果,如您所说,您的所有视图都基于 UserControl,那么我们可以使用 ContentControl 对象来显示它们,前提是相关的 DataTemplate 对象存在.

Now onto view inheritance... as you know, there is no such thing in WPF, but there (kind of) is a way around this problem. If, as you say, all of your views are UserControl based, then we can use ContentControl objects to display them, provided that relevant DataTemplate objects exist.

这基本上意味着,如果您想使用特定的 Window 作为视图的外部模板,那么您可以添加一个 ContentControl 来显示视图.例如,我有一个动画 Window,它显示为带有按钮的对话控件,我将几个不同的 UserControl 绑定到这些按钮,以便它们都具有相同的整体外观.

This basically means that if you want to use a particular Window as an outer template for your views, then you can if you add a ContentControl to display the view. As an example, I have an animated Window that appears as a dialogue control with buttons which I bind several different UserControls to, so that they all have the same overall appearance.

在 xaml 中:

...
<Border CornerRadius="3.5" BorderBrush="{StaticResource TransparentBlack}" 
    BorderThickness="1" Padding="1">
    <ContentControl Content="{Binding ViewModel, RelativeSource={RelativeSource 
        FindAncestor, AncestorType={x:Type Controls:AnimationWindow}}}" />
</Border>
...

而在Window后面的代码:

public static readonly DependencyProperty ViewModelProperty = DependencyProperty.
    Register("ViewModel", typeof(BaseViewModel), typeof(AnimationWindow));

public BaseViewModel ViewModel
{
    get { return (BaseViewModel)GetValue(ViewModelProperty); }
    set { SetValue(ViewModelProperty, value); }
}

使用这个类,我可以在 xaml 中绑定到这个 ViewModel 属性,或者从通过构造函数传入的值设置它.由于我的所有视图模型都扩展了这个 BaseViewModel 类,我可以将此属性设置为其中任何一个.

Using this class, I can either bind to this ViewModel property in xaml, or set it from a value passed in through a constructor. As all of my view models extend this BaseViewModel class, I can set this property to any of them.

更新>>>

使用ContentControlDataTemplate 来连接视图/视图模型的要点是我们摆脱了硬编码".我可以在每个应用程序中使用这个设置,而且我依赖于使用任何特定的实现.

The point of using the ContentControl and the DataTemplates to connect the views/view models is that we get away from 'hard coding'. I can use this setup in every application and I'm not tied to using any particular implementation.

现在,如果你说你真的想在所有不同的应用程序中使用相同视图/视图模型配对,那么你应该把你的DataTemplates在单独的 Resources xaml 文件中连接视图/视图模型.这样,您可以将此文件合并到您需要的应​​用程序中的 App.xaml Resources 部分:

Now if you're saying that you actually want to use the same view/view model pairings in all of your different applications, then you should put your DataTemplates to connect the views/view models in a separate Resources xaml file. That way, you can merge this file into your App.xaml Resources section in applications where you need to and not when you don't:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="ViewModelToViewDataTemplates.xaml"/>
      <ResourceDictionary>
          <!-- Add your normal resources here -->
      </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

这篇关于如何在应用程序中使用框架中的视图?(WPF MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆