我是WPF和MVVM的新手,在尝试DataContext在两个单独的视图中设置我的ViewModel的同一个实例时遇到了一个问题.
这是因为:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
Run Code Online (Sandbox Code Playgroud)
将为每个视图创建一个新的视图模型实例.
为了解决这个问题,我决定创建一个存储我使用的每个ViewModel的静态实例的类.然后在cs每个视图的文件中,我将从此DataContext静态类设置为适当的ViewModel.
这可行,但对于可能同时需要ViewModel的多个实例的大型程序而言似乎不是最佳选择.
有什么更好的方法来解决这个问题 - 是否有合理的方法可以使用ViewModel的同一个实例来拥有多个视图?
或者这种做法是不好的做法 - 我应该为每个ViewModel设计一个带有一个View的程序吗?
谢谢!
这里的代码:
#include <stdio.h>
int main(void) {
test(7.4, 4);
return 0;
}
void test(float height, float radius){
printf("%f", height);
}
Run Code Online (Sandbox Code Playgroud)
将打印:
0.000000
Run Code Online (Sandbox Code Playgroud)
为什么是这样?为什么不打印7.4?
我决定尝试创建一个连接函数,因为 strcat 不适用于字符,仅适用于字符串。
\n\n#include <stdio.h>\n#include <string.h>\n\nchar concat(char a[], char b[]);\n\nint main ()\n{\n char *con = concat("hel", "lo");\n return(0);\n}\n\nchar concat(char a[], char b[]){\n int lena = strlen(a);\n int lenb = strlen(b);\n char con[lena+lenb];\n con[0] = a;\n con[lena] = b;\n printf("%s", con);\n return con;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n此代码打印“\xc3\x83\xe2\x80\xa6\xc3\x83\xc3\x86”。不确定我哪里出错了?
\n\n谢谢
\n