我正在尝试设计时数据服务,以便在XAML中使用一些数据。 我正在使用OpenhardwareMonitor库来获取PC的硬件信息。
我的代码可以提取“SensorType.Power”类型的所有传感器,并在ItemsControl中显示它们:
<ItemsControl
ItemsSource="{Binding Sensors}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Height="100"
Margin="5,5,5,0"
Grid.Row="0">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type models:HardwareSensorCollection}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Hardware, Converter={StaticResource HardwareNameTypeConcatConverter}}" Grid.Row="0" />
<ItemsControl ItemsSource="{Binding PowerSensors}" DisplayMemberPath="Name" Grid.Row="1" Margin="5,0,0,0" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
获取传感器的代码(在运行期间)在我的视图的Loaded事件中执行。在设计时,我只是将我的视图模型称为Loaded()方法。我正在使用MVVM,所以我使用以下内容将其传递给我的视图模型:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
获取传感器的代码很好,并且在运行时完美运行。但是,在设计时执行时,它会在Visual Studio的设计视图中生成以下错误:
FileNotFoundException: Could not load file or assembly 'OpenHardwareMonitorLib, Version=0.7.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
at PowerMonitor.DesignData.DesignDataService.<>c__DisplayClass0.<GetHardwareSensorCollections>b__1()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at PowerMonitor.ViewModels.MainViewModel.<Loaded>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
我在VS 2015工作,但VS 2013中出现同样的错误。
我想,也许它在运行设计时间时无法获得传感器,所以我创建了几个继承IHardware和ISensor的类,以便我可以像这样初始化它:
var hardwareCollection1 = new HardwareSensorCollection(new DesignHardware("Example Hardware 1", HardwareType.CPU));
hardwareCollection1.PowerSensors.Add(new DesignSensor("Example Sensor 1", SensorType.Power));
hardwareCollection1.PowerSensors.Add(new DesignSensor("Example Sensor 2", SensorType.Power));
var hardwareCollection2 = new HardwareSensorCollection(new DesignHardware("Example Hardware 2", HardwareType.CPU));
hardwareCollection2.PowerSensors.Add(new DesignSensor("Example Sensor 1", SensorType.Power));
hardwareCollection2.PowerSensors.Add(new DesignSensor("Example Sensor 2", SensorType.Power));
hardwareCollection2.PowerSensors.Add(new DesignSensor("Example Sensor 3", SensorType.Power));
hardwareCollection2.PowerSensors.Add(new DesignSensor("Example Sensor 4", SensorType.Power));
return new List<HardwareSensorCollection> { hardwareCollection1, hardwareCollection2 };
但这给了我同样的错误。
我发现了几个有类似问题的人的问题,但他们的解决方案都没有对我有用。我想我必须把这个DLL放在某处,以便VS在调用设计时代码时能找到它。
有什么想法吗?
答案 0 :(得分:0)
如果您在 XAML 设计器中遇到此类问题(无法加载文件或程序集),可能是由于平台造成的。我收到了这样的消息:
Attempting to load a program with an incorrect format.
在我的输出窗口中,我通过在我的解决方案配置中从 AnyCPU 切换到 x64 解决了问题。