Я использую WPF + MVVMlight.
Я создаю класс с именем 'JSON' и создаю экземпляр в SimpleIO C.
Получите путь с помощью Path.GetFullPath в конструкторе 'JSON'.
Программа работает нормально. Тем не менее ошибка Intellisense происходит в App.xaml.
"vm: ViewModelLocator x: Key = "Locator" d: IsDataSource = "True"
подчеркивается и возникает следующая ошибка.
"Не удалось найти файл 'C: \ Users \ john \ Desktop \ Count \ Setting. json '. "
Не является ли Path.GetFullPath функцией, которая получает путь к текущему исполняемому файлу? Почему должен быть файл в пути 'C: \ Users \ john \ Desktop \ Count \ Setting. json'?
Когда я удаляю "Path.GetFullPath (" Setting. json ");" из класса JSON ошибка IntelliSense исчезает в App.xaml.
о! еще одна вещь, даже когда MainWindow.xaml
открыт, такая же ошибка возникает в App.xaml.
мой путь к папке решения - "C: \ Users \ john \ Desktop \ Count".
Мой путь к файлу excution: "C: \ Users \ john \ Desktop \ Count \ Count \ bin".
В App.xaml
<Application x:Class="Count.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Count"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="3.Driverlayer/MainWindow.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Count">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
</Application>
В ViewModelLocator.cs
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view services and models
//// SimpleIoc.Default.Register<IDataService, DesignDataService>();
////}
////else
////{
//// // Create run time view services and models
//// SimpleIoc.Default.Register<IDataService, DataService>();
////}
SimpleIoc.Default.Register<ISettingRead, JSON>(true);
SimpleIoc.Default.Register<MainViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
In JSON .cs
class JSON : ISettingRead
{
string _path;
string _jsonString;
JObject jsonobj;
public JSON()
{
_path = Path.GetFullPath("Setting.json");
using (StreamReader sr = new StreamReader(_path))
{
_jsonString = sr.ReadToEnd();
}
jsonobj = JObject.Parse(_jsonString);
}
}