Ho deciso di utilizzare la libreria MVVM Light per aiutare a progettare un'interfaccia utente. Dopo tonnellate di ricerche e tentativi ed errori, devo ancora trovare le risposte che sto cercando. Ho cercato su Google e letto ogni domanda StackOverflow che riesco a trovare, tuttavia, il mio problema sembra essere unico su SO.MVVM Light - Controlli utente come viste
Desidero progettare un'interfaccia utente con una singola finestra e popolarla con diversi Views/UserControls. NON voglio una barra di navigazione condivisa tra gli UserControls né voglio che vengano visualizzate più finestre. Ogni View/UserControl dovrebbe collegarsi al proprio ViewModel, mentre MainWindow si collegherà a un MainViewModel.
Scenario di esempio - MainWindow con 3 controlli utente
1. MainWindow populates with first UserControl which has a listbox and 3 buttons, the first button is enabled.
2. User clicks the first button.
3. MainWindow populates with second UserControl.
Oppure, in aggiunta
2. User selects choice from a listbox, button two and three become available.
3. User clicks second/third button.
4. MainWindow populates with second/third UserControl.
Ecc, ecc
Forse il mio approccio non è realistico, ma mi sento questo deve Essere possibile. Non capisco come far funzionare tutti questi pezzi concettualmente. Non c'è modo in cui i miei desideri siano unici. Se ritieni che si tratti di una domanda doppia, reindirizza. Saluti.
Per rendere le cose più facili da capire, ho incluso alcune classi di seguito. Innanzitutto, la mia app.xaml.
<Application x:Class="Bobcat_BETA.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:Bobcat_BETA.UserControls"
xmlns:vm="clr-namespace:Bobcat_BETA.ViewModels"
StartupUri="MainWindow.xaml"
mc:Ignorable="d">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<DataTemplate DataType="{x:Type vm:SavedScenariosViewModel}">
<views:SavedScenariosUserControl />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ScenarioEditorViewModel}">
<views:ScenarioEditorUserControl />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SimulatorViewModel}">
<views:SimulatorUserControl />
</DataTemplate>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="Bobcat_BETA.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bobcat - Version:0.00"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
<ContentControl Content="{Binding CurrentView}"/>
</Grid>
ViewModelLocator.cs
namespace Bobcat_BETA.ViewModels
{
public class ViewModelLocator
{
private static MainViewModel _main;
public ViewModelLocator()
{
_main = new MainViewModel();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get
{
return _main;
}
}
}
}
MainViewModel.cs
namespace Bobcat_BETA.ViewModels
{
public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentViewModel;
readonly static SavedScenariosViewModel _savedScenarioViewModel = new SavedScenariosViewModel();
readonly static ScenarioEditorViewModel _scenarioEditorViewModel = new ScenarioEditorViewModel();
readonly static SimulatorViewModel _simulatorViewModel = new SimulatorViewModel();
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}
public MainViewModel()
{
CurrentViewModel = MainViewModel._savedScenarioViewModel;
SavedScenarioViewCommand = new RelayCommand(() => ExecuteSavedScenarioViewCommand());
ScenarioEditorViewCommand = new RelayCommand(() => ExecuteScenarioEidtorViewCommand());
SimulatorViewCommand = new RelayCommand(() => ExecuteSimulatorViewCommand());
}
public ICommand SavedScenarioViewCommand { get; private set; }
public ICommand ScenarioEditorViewCommand { get; private set; }
public ICommand SimulatorViewCommand { get; private set; }
private void ExecuteSavedScenarioViewCommand()
{
CurrentViewModel = MainViewModel._savedScenarioViewModel;
}
private void ExecuteScenarioEidtorViewCommand()
{
CurrentViewModel = MainViewModel._scenarioEditorViewModel;
}
private void ExecuteSimulatorViewCommand()
{
CurrentViewModel = MainViewModel._simulatorViewModel;
}
}
}
SavedScenariosViewModel.cs
namespace Bobcat_BETA.ViewModels
{
public class SavedScenariosViewModel : ViewModelBase
{
public SavedScenariosViewModel()
{
}
ObservableCollection<ScenarioModel> _scenarioModels = new ObservableCollection<ScenarioModel>()
{
new ScenarioModel() {Name = "Scenario 0", ID = 000, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 1", ID = 001, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 2", ID = 002, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 3", ID = 003, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 4", ID = 004, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 5", ID = 005, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 6", ID = 006, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 7", ID = 007, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 8", ID = 008, Desc = "This will describe the Scenario Model."},
new ScenarioModel() {Name = "Scenario 9", ID = 009, Desc = "This will describe the Scenario Model."}
};
public ObservableCollection<ScenarioModel> ScenarioModels
{
get { return _scenarioModels; }
}
}
}
SavedScenariosUserControl.xaml
<UserControl x:Class="Bobcat_BETA.UserControls.SavedScenariosUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:Bobcat_BETA.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionaries/MasterDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
<DynamicResource ResourceKey="GeneralUserControl"/>
</UserControl.Style>
<Grid>
<Label Content="Saved Scenario Selection"
Style="{StaticResource GeneralLabel}" HorizontalAlignment="Left" Margin="26,30,0,0" VerticalAlignment="Top" Height="62" Width="345"/>
<Label Content="Chose Flight Model:"
Style="{StaticResource GeneralLabel2}"
HorizontalAlignment="Left" Margin="27,111,0,0" VerticalAlignment="Top" Height="43" Width="345"/>
<ListBox Style="{StaticResource GeneralListBox}"
HorizontalAlignment="Left" Height="509" Margin="27,154,0,0" VerticalAlignment="Top" Width="345"
ItemsSource="{Binding ScenarioModels}"/>
<Button Content="New"
Style="{StaticResource TransitionButton}"
HorizontalAlignment="Left" Margin="948,601,0,0" VerticalAlignment="Top" MinHeight="62" MinWidth="150" IsEnabled="True"
Command="{Binding ScenarioEditorViewCommand}"/>
<Button Content="Edit"
Style="{StaticResource TransitionButton}"
HorizontalAlignment="Left" Margin="401,519,0,0" VerticalAlignment="Top" MinHeight="62" MinWidth="150"
Command="{Binding SaveScenariosViewCommand}"/>
<Button Content="Load"
Style="{StaticResource TransitionButton}"
HorizontalAlignment="Left" Margin="401,601,0,0" VerticalAlignment="Top" MinHeight="62" MinWidth="150"
Command="{Binding SimulatorViewCommand}"/>
</Grid>
</UserControl>
Se qualcosa non è chiaro, posso aggiungere le classi del modello pure, ma presumo si può fare inferenze da ciò che sta accadendo. Grazie.
In realtà ho la mia App.xaml presentata esattamente come il primo esempio che hai menzionato. Invece di ricavare i miei ViewModels da BaseViewModel, provengo da ViewModelBase di MVVM Light. Sono un po 'perso su Messenger però. Domani pubblicherò tutte le mie lezioni a cui tutti potranno dare un'occhiata. – piofusco
Ok! Mi hai perso un po 'su come si ottiene ViewModelBase da MVVM Light. Ma sì, pubblica il tuo codice e darò un'occhiata. – Stunna
Aggiunto un po 'di codice sopra. – piofusco