让我用这个问题作为前缀,说明我对C#和WPF都很新.
我正在尝试将一组Boolean值连接到一个包含6个复选框的容器,并在按下按钮时存储这些值的状态.我假设有一种简单的方法可以做到这一点,因为绑定到一个集合的复选框似乎是很自然的事情,但到目前为止我看到的所有解决方案似乎都过于复杂(例如:http:// merill. net/2009/10/wpf-checked-listbox /).
我通过修改的数据模板创建的复选框ListBox,设置ItemsSource的ListBox到ObservableCollection,但我的问题是,我不知道该怎么绑定IsChecked到,因为我试图将其绑定到实际对象的集合而不是对象的属性.
我已经阅读了一些关于volatile没有此关键字的关键字和行为的帖子
我特别测试了C#中说明volatile关键字的使用答案的代码.运行时,我会在发布模式下观察异常行为,不附加调试器.到那时为止,没有问题.
所以,据我所知,以下代码永远不会退出.
public class Program
{
private bool stopThread;
public void Test()
{
while (!stopThread) { } // Read stopThread which is not marked as volatile
Console.WriteLine("Stopped.");
}
private static void Main()
{
Program program = new Program();
Thread thread = new Thread(program.Test);
thread.Start();
Console.WriteLine("Press a key to stop the thread.");
Console.ReadKey();
Console.WriteLine("Waiting for thread.");
program.stopThread = true;
thread.Join(); // Waits for the thread to stop.
}
}
Run Code Online (Sandbox Code Playgroud)
为什么退出?即使在发布模式下,没有调试器?
修改了C#中对volatile关键字的使用的代码.
private bool …Run Code Online (Sandbox Code Playgroud) 我有这个foreach部分,我试图在我的"result = string.Format"之后添加一行但是我得到以下错误"只有赋值,调用,递增,递减,等待和新对象表达式可以用作语句"有人能告诉我我做错了什么.
foreach (DataRow record in table.Rows)
{
string key = record["Code"] + "_" + record["Description"];
int row = (int)rownum[key];
string date = formatDate(record["ApptDate"].ToString(), "_");
string result = string.Empty;
if (record["Dosage"].ToString() != string.Empty)
result = string.Format("{0}/{1}", test.SharedLib.Application.RemoveTrailingZeroes(record["Dosage"].ToString()),
test.SharedLib.Application.RemoveTrailingZeroes(record["Units"].ToString()));
if (record["Dosage"].ToString() != string.Empty)
result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result;
else if (record["Units"].ToString() != string.Empty)
result = record["Units"].ToString();
dataTable.Rows[row]["ApptDate" + date] = result;
}
Run Code Online (Sandbox Code Playgroud) 我正在做一些基本的编码练习,但是有一个问题困扰着我.我需要从标准输入读取一个随机数的整数并将它们存储在一个向量中.然后读取第二个随机数的整数并将它们存储在另一个整数中.
这是我的代码:
vector<int> i_vect1, i_vect2;
int itemp;
cout<<"Input numbers into vector1?"<<endl;
while(cin>>itemp){
i_vect1.push_back(itemp);
}
cout<<"Input numbers into vector2?"<<endl;
while(cin>>itemp){
i_vect2.push_back(itemp);
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我输入数字1 2 3 4 5,然后按ctrl+z结束我的输入vector1.但它无法读取整数vector2,程序结束了.
我该怎么办?我的代码出了什么问题?