0

我是 c# 和 uwp 应用程序的新手。这是我第一次尝试使用 MVVM。我有以下程序设计并根据 FDD 的架构进行。 MVVM 应用程序设计

我想实现:

  • 在 ViewMain.xaml 上注册 ViewFeature.xaml
  • 特征之间的通信。

我有以下简单的方法:通过代码将 ViewFeature.xaml 添加到 ViewMain.xaml,不幸的是目前不起作用。

  • 查看注册:

主视图.xaml

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="using:Client.ViewModel"
x:Class="Client.View.ViewMain"
mc:Ignorable="d">

<Grid x:Name="ContentGrid" RequestedTheme="Light">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <StackPanel Name="FeatureLeft"      Orientation="Horizontal" Grid.Column="0"/>
    <StackPanel Name="FeatureCenter"    Orientation="Horizontal" Grid.Column="1"/>
    <StackPanel Name="FeatureRight"     Orientation="Horizontal" Grid.Column="2"/>
</Grid>
</Page>

ViewMain.xaml.cs

public sealed partial class ViewMain : Page
{

    public ViewModelMain viewModelMain;

    public ViewMain()
    {           
        this.InitializeComponent();
        viewModelMain = new ViewModelMain();
        viewModelMain.RegisterFeatures(this);        
    }
}

ViewModelMain.cs

public class ViewModelMain: NotificationBase
{
    public ModelMain model;
    public ViewModelMain()
    {
        model = new ModelMain();
        _Features = model.LoadFeatures();
    }

    public void RegisterFeatures(Page p)
    {
        foreach (var feature in _Features)
        {
            feature.AddToView(p);
        }
    }

    ObservableCollection<IViewFeature> _Features = new ObservableCollection<IViewFeature>();
    public ObservableCollection<IViewFeature> Features {
        get { return _Features; }
        set { SetProperty(ref _Features, value); }
    }

}

模型Main.cs

public class ModelMain
{

    public ObservableCollection<IViewFeature> FeatureList;


    public ObservableCollection<IViewFeature> LoadFeatures()
    {
        FeatureList = new ObservableCollection<IViewFeature>();
        IViewFeature galleryFeature = new Gallery.View.ViewGallery();
        FeatureList.Add(galleryFeature);

        return FeatureList;
    }

}

每个 Feature 都知道自己在 ViewMain.xml 上的位置,并在 ViewFeaure.xaml.cs 中实现以下方法在 ViewMain.xaml 上注册自己的 ViewFeature.xaml

public void AddToView( Page p)
    {
        StackPanel target = (StackPanel)p.FindName("FeatureLeft");
        target.Children.Add(this);
    }

任何专业的方法或帮助将不胜感激。

4

1 回答 1

0

问题出在这一行。

target.Children.Add(this);

应该

target.Children.Add(this as UIElement);

但是,我仍然想知道在功能驱动开发 (FDD) 的上下文中,基于功能的应用程序的专业方法可能看起来如何。

于 2017-07-28T19:56:21.717 回答