이 질문은 다소 기본적인 것이지만 현재로서는 새로운 ViewModel을 MVVM
Light Toolkit의 ViewModelLocator 클래스에 추가해야하는 방법을 완전히 잃어 버렸습니다.MVVM Light Toolkit의 ViewModelLocator에 새 ViewModel 추가
나의 현재 구현과 같이 보입니다 :
먼저 내가 Settings
라는 이름의 창을 가지고 있다고 가정하는 뷰 모델은 SettingsViewModel
과 ViewModelLocator ViewModelLocator
을 지명했다.
먼저 나는 VieModelLocator 생성자에서 CreateSettings()
를 호출이 항상 내가 조화를 사용하지 않는로 실행하고 응용 프로그램을 내가 그것을 실행하려고 할 때마다 구축 할 것입니다
public ViewModelLocator()
{
if (ViewModelBase.IsInDesignModeStatic)
{
}
else
{
CreateSettings();
}
CreateMain();
}
하는 것으로. 이제`CreateSettings() 메소드에 대해서.
내가 뭘하고 있었는지 전혀 알지 못했기 때문에 MainViewModel을 만들고 관리하는 데 사용한 메서드를 모두 안전하게 실행하고 모델을 만들려고했습니다.
public static void CreateSettings()
{
if (_settings == null)
{
_settings = new SettingsViewModel();
}
}
그런 다음 MainViewModel에 사용되는 모델로 또 다른 몇 가지 방법 :
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public SettingsViewModel Settings
{
get
{
return SettingsStatic;
}
}
public static SettingsViewModel SettingsStatic
{
get
{
if (_settings == null)
{
CreateSettings();
}
return _settings;
}
}
그리고 내 Settings
창 XAML에서 : 창은 다음과 같이 내 MainViewModel에서 열린
<Window x:Class="_5500A_Auto_Calibrator.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Settings" Height="300" Width="300"
DataContext="{Binding Source={StaticResource Locator}, Path=Settings}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
:
Settings settings = new Settings();
settings.Show();
나는
Locator
자원을 찾을 수있는 윈도우의 무능력과 관련된 오류를 읽었
"Cannot find resource named 'Locator'. Resource names are case sensitive."
:하지만,
"'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '4' and line position '39'."
와의 내부 예외 :이하려고하면
, 나는 예외가 나타날 대부분 블렌드와 관련이 있습니다.
현재 제가하고있는 일은 제가 잘못하고있는 것이지만 새로운 뷰 모델을 추가하는 것에 대한 문서가 너무 적어서 제가 잘못하고있는 것이 확실하지 않습니다.
편집 :
내 App.Xaml :
<Application x:Class="_5500A_Auto_Calibrator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:_5500A_Auto_Calibrator.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
StartupUri="MainWindow.xaml"
mc:Ignorable="d">
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
</Application.Resources>
</Application>
안녕하세요, 단테,이 대답은 당신의 질문에 대한 답 이었습니까? (대답이있는 경우 표시 할 수 있습니다. 그렇지 않은 경우 더 자세히 설명 할 수 있습니다.) – Noctis