2017-07-28 44 views
0

我是c#和uwp应用程序的新手。这是我第一次尝试使用MVVM。 我有以下程序设计,并根据FDD的架构去。 MVVM App-Design使用MVVM的UWP应用程序:在主视图上注册功能视图

我想实现:在ViewFeature.xaml对特征之间ViewMain.xaml

  • 通信

    • 注册。

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

    • 查看报名方式:

    MainView.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); } 
        } 
    
    } 
    

    ModelMain.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; 
        } 
    
    } 
    

    的每个功能都知道自己在ViewMain.xml位置和ViewFeaure.xaml.cs实现以下的方法对ViewMain.xaml

    注册自己ViewFeature.xaml
    public void AddToView(Page p) 
        { 
         StackPanel target = (StackPanel)p.FindName("FeatureLeft"); 
         target.Children.Add(this); 
        } 
    

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

  • +0

    您是否收到任何错误?什么不工作? – user1777136

    +0

    @ user1777136主视图为空,并且没有错误消息。应该有一个按钮,它打开图库的详细视图。 – Calid

    回答

    0

    问题出在这一行。

    target.Children.Add(this); 
    

    应该

    target.Children.Add(this as UIElement); 
    

    不过,我还是想知道专业的方法如何基于特征的应用程序可能看起来在功能驱动开发(FDD)的情况下。

    +0

    你的代码看起来很合理。我会避而远之的是在“AddToView”方法内对StackPanel进行“硬编码”。绑定机制可以让你摆脱这一点。 MVVM严重依赖绑定来从视图中分离代码。所以例如''可以删除整个'RegisterFeatures'和'AddToView'方法。 – user1777136