2017-07-28 3 views
0

私はc#およびuwp appsを初めて使用しています。これはMVVMを使った初めての試みです。 私は以下のプログラム設計をしており、FDDのスキーマに従って進んでいます。 MVVM App-DesignMVVMを使用したUWPアプリケーション:メインビューでの機能ビューの登録

私が達成したいと思います:ViewFeature.xamlの

  • 登録機能とのViewMain.xaml
  • 通信に。

私は以下の簡単なアプローチをとっています:ViewMeam.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

関連する問題