我有一个包含UserControl的MainWindow,它们都是用MVVM模式实现的.MainWindowVM具有我想要绑定到UserControl1VM中的属性的属性.但这不起作用.
这里是一些代码(viewmodels使用某种mvvm框架,在ViewModelBase类中实现INotifyPropertyChanged,但希望没问题):
MainWindow.xaml:
<Window x:Class="DPandMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DPandMVVM"
Title="MainWindow" Height="300" Width="300">
<Grid>
<local:UserControl1 TextInControl="{Binding Text}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
CodeBehind MainWindow.xaml.cs:
using System.Windows;
namespace DPandMVVM
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
}
Run Code Online (Sandbox Code Playgroud)
MainWindow-ViewModel MainWindowVM.cs:
namespace DPandMVVM
{
public class MainWindowVM : ViewModelBase
{
private string _text;
public string Text { get { return _text; } }
public MainWindowVM()
{
_text = "Text from MainWindowVM";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是UserControl1.xaml:
<UserControl …Run Code Online (Sandbox Code Playgroud) 我发现很多关于 ViewModels 及其属性的讨论比较了两种方法:INotifyPropertyChanged 的实现或通过Dependency Properties 的实现。
虽然我做了很多 INotifyPropertyChanged(并且它正在工作),但我在实施 DP 方法时遇到了困难。
当我像这样在 ViewModel 中注册 DP 时
public static readonly DependencyProperty SomePropertyProperty =
DependencyProperty.Register("SomeProperty", typeof(string), typeof(MyUserControl));
Run Code Online (Sandbox Code Playgroud)
并尝试在某处使用它:
<myNameSpace:MyUserControl SomeProperty="{Binding ...}"/>
Run Code Online (Sandbox Code Playgroud)
有一个编译器错误:
The property 'SomeProperty' does not exist in XML namespace 'clr-namespace:myNameSpace'.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么??
编辑1
ViewModel 看起来像这样:
public class MyUserControlVM : DependencyObject
{
public string SomeProperty
{
get { return (string)GetValue(SomePropertyProperty); }
set { SetValue(SomePropertyProperty, value); }
}
public static readonly DependencyProperty SomePropertyProperty =
DependencyProperty.Register("SomeProperty", typeof(string), typeof(MyUserControl));
}
Run Code Online (Sandbox Code Playgroud) 我遇到了想要写入.exe.config的应用程序的问题.
请参阅以下代码:
using System;
using System.Configuration;
using System.IO;
namespace ConfigurationManagerError
{
class Program
{
static void Main(string[] args)
{
try
{
// set Config-File to persistent folder
ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap();
exeMap.ExeConfigFilename = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"ConfigError\\ConfigurationManagerError.exe.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add(
new KeyValueConfigurationElement("TestKey", "TestValue"));
config.Save(ConfigurationSaveMode.Modified);
Console.WriteLine("Successfully write of Configuration-File to " + config.FilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
只要我作为具有读写访问权限的用户运行该文件夹一切正常.如果我的用户在该文件夹中没有写入权限,则会出现一个异常,指出不允许写入.exe.config的权限.到目前为止,一切都如预期.
但是,如果我现在有一个用户有权写入现有文件而不是创建新文件,则抛出异常
Access to the path 'C:\ProgramData\ConfigError\ila2ba_0.tmp' is denied
Run Code Online (Sandbox Code Playgroud)
似乎ConfigurationManager想要创建一个tmp-File.怎么解决这个问题? …
我正在编写单元测试并考虑实现类型的场景IClonable.所以当然我想要一个测试Clone()方法的单元测试.
[Test]
public void CloneObject()
{
MyType original = new MyType();
MyType clone = (MyType)original.Clone();
// Assert for equality
}
Run Code Online (Sandbox Code Playgroud)
所以我的第一个任务是拥有Assert平等.我看到以下选项:
MyType并逐个检查它们Equals()in MyType以MyType说明两个实例是否相等(考虑到有时测试的相等性被视为生产代码的相等性)MyType必须如此[Serializable],但如果它具有例如接口属性,有时很难做到)对于前两个我可以设置我的测试,他们运作良好.但是,如果我更改MyType并添加其他属性怎么办?如果Clone()不复制这个并且我不将它添加到checked属性列表或equals方法中,即使没有复制属性,我的测试仍会通过.
你是如何解决这种测试的?