I am new to c# and uwp apps. And this is my first try with MVVM. I have the following program design and go according to the schema of the FDD. MVVM App-Design
I would like to achieve:
- Registration of the ViewFeature.xaml on ViewMain.xaml
- communication between features.
I have the following simple Approach: Add the ViewFeature.xaml to ViewMain.xaml by Code, which unfortunately does not work at the moment.
- View Registration:
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;
}
}
Each Feature knows his own position on the ViewMain.xml and implements in the ViewFeaure.xaml.cs the following method to register its own ViewFeature.xaml on the ViewMain.xaml
public void AddToView( Page p)
{
StackPanel target = (StackPanel)p.FindName("FeatureLeft");
target.Children.Add(this);
}
Any professional approach or help will be appreciated.