我有Json字符串,如下所示
{
"JsonValues":{
"id": "MyID",
"values": {
"value1":{
"id": "100",
"diaplayName": "MyValue1"
},
"value2":{
"id": "200",
"diaplayName": "MyValue2"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想将Json字符串转换为以下类
class ValueSet
{
[JsonProperty("id")]
public string id
{
get;
set;
}
[JsonProperty("values")]
public List<Value> values
{
get;
set;
}
}
class Value
{
public string id
{
get;
set;
}
public string DiaplayName
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
我的反序列化代码是
JavaScriptSerializer js = new JavaScriptSerializer();
StreamReader sr = new StreamReader(@"ValueSetJsonString.txt");
string jsonString = sr.ReadToEnd();
var items = JsonConvert.DeserializeObject<ValueSet>(jsonString); …Run Code Online (Sandbox Code Playgroud) 在等待API帖子完成我如何解决这个问题时,AggregateException正在抛出?
我的API调用类似于此
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(workflowUrl);
var task = httpClient.PostAsJsonAsync("api/apiname/execute/", executeModel)
.ContinueWith(x => x.Result.Content.ReadAsAsync<bool>().Result);
Task continuation = task.ContinueWith(x =>
{
bool response = x.Result;
});
continuation.Wait();
}
Run Code Online (Sandbox Code Playgroud)
在等待POST完成时,我得到了Exception.
System.AggregateException was caught
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at
InnerException: System.AggregateException
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.get_Result()
at
at System.Threading.Tasks.Task`1.<>c__DisplayClass17.<ContinueWith>b__16(Object obj)
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
InnerException: System.AggregateException …Run Code Online (Sandbox Code Playgroud) 我想将一千个分隔值转换为整数,但我得到一个例外.
double d = Convert.ToDouble("100,100,100");
Run Code Online (Sandbox Code Playgroud)
工作正常,越来越好 d=100100100
int n = Convert.ToInt32("100,100,100");
Run Code Online (Sandbox Code Playgroud)
得到一个格式异常
输入字符串的格式不正确
为什么?
我是MVVM的新手.学习我创建了一个示例应用程序,以便在单击按钮时在文本框中显示消息.在我的代码中,button命令工作正常,但属性未绑定到Textbox.如何使用MVVM将Property绑定到Textbox?
我的代码类似于下面给出的.
视图
<TextBox Name="MessageTextBox" Text="{Binding TestMessage}"/>
<Button Content="Show" Name="button1" Command="{Binding ShowCommand}">
<!-- Command Handler -->
</Button>
Run Code Online (Sandbox Code Playgroud)
查看模型
MyMessage myMessage;
public MainViewModel()
{
myMessage=new MyMessage();
}
//inside the ShowCommand Handler
TestMessage="Hello World";
// A Property to set TextBox Value.
Run Code Online (Sandbox Code Playgroud)
模型
public class MyMessage: INotifyPropertyChanged
{
private string testMessage;
public string TestMessage
{
get { return testMessage; }
set
{
testMessage= value;
OnPropertyChanged("TestName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new …Run Code Online (Sandbox Code Playgroud) 我有一个超过20k迭代的for循环,每次迭代需要大约两到三秒,总共大约20分钟.我如何优化这个循环.我正在使用.net3.5所以并行foreach是不可能的.所以我将200000 nos分成小块并实现了一些线程,现在我可以将时间缩短50%.有没有其他方法来优化这种for循环.
我的示例代码如下
static double sum=0.0;
public double AsyncTest()
{
List<Item> ItemsList = GetItem();//around 20k items
int count = 0;
bool flag = true;
var newItemsList = ItemsList.Take(62).ToList();
while (flag)
{
int j=0;
WaitHandle[] waitHandles = new WaitHandle[62];
foreach (Item item in newItemsList)
{
var delegateInstance = new MyDelegate(MyMethod);
IAsyncResult asyncResult = delegateInstance.BeginInvoke(item.id, new AsyncCallback(MyAsyncResults), null);
waitHandles[j] = asyncResult.AsyncWaitHandle;
j++;
}
WaitHandle.WaitAll(waitHandles);
count = count + 62;
newItemsList = ItemsList.Skip(count).Take(62).ToList();
}
return sum;
}
public double MyMethod(int id)
{
//Calculations …Run Code Online (Sandbox Code Playgroud) 根据Grady Booch的"面向对象分析和设计",没有继承的编程不是面向对象的,被称为使用抽象数据类型的编程.如果使用c#开发一个带有继承的类的应用程序,这是面向对象的(因为语言是面向对象的)还是不是?
我有一个类似下面的linq查询.
IEnumerable<DataRow> query= (from item in IItemsTable.AsEnumerable()
where somecondition
select item);
Run Code Online (Sandbox Code Playgroud)
如何检查查询是否包含任何行或为空?
我正在使用MVVM工具包版本1.我有两个文本框textbox1和textbox2.我需要在按下按钮时将这两个值作为参数传递,并且需要在名为textbox3的第三个文本框上显示结果.
我的VM代码与此类似
public ICommand AddCommand
{
get
{
if (addCommand == null)
{
addCommand = new DelegateCommand<object>(CommandExecute,CanCommandExecute);
}
return addCommand;
}
}
private void CommandExecute(object parameter)
{
var values = (object[])parameter;
var a= (int)values[0];
var b= (int)values[1];
Calculater calcu = new Calcu();
int c = calcu.sum(a, b);
}
private bool CanCommandExecute(object parameter)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
当用户单击按钮但我的参数参数没有任何值时,将调用commandExecute方法.我如何将用户的值作为参数传递?并将结果返回到texbox3?
我想获取我的exe的当前工作目录.Directory.GetCurrentDirectory()部署应用程序时不返回exe的工作目录.
获取当前工作目录的方法是什么?
我已经实现了类似于以下示例的后台工作人员类,并且我想在每次后台工作人员完成时更新我的 UI。
for (int i = 1; i < 10; i++)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
worker.RunWorkerAsync(i);
while (worker.IsBusy == true)
{
Thread.Sleep(100);
}
}
Run Code Online (Sandbox Code Playgroud)
Worker_DoWork 返回数据行和 Worker_RunWorkerCompleted 正在将返回的结果添加到数据网格。但该函数永远不会以正确的顺序到达 Worker_RunWorkerCompleted。我怎么能解决这个问题?
编辑:
为了清楚起见,我正在更新更多细节。
<my:DataGrid x:Name="theGrid" RowHeight="30" ItemsSource="{Binding Category}" AutoGenerateColumns="True" HeadersVisibility="All" Margin="235,96.5,84,65.5">
<my:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Expander>
<my:DataGrid Height="300" ItemsSource="{Binding Products}" AutoGenerateColumns="True" HeadersVisibility="Column"> </my:DataGrid>
</Expander>
</DataTemplate>
</my:DataGrid.RowDetailsTemplate>
</my:DataGrid>
//List of objects
List<Category> Categories = new List<Category>();
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int …Run Code Online (Sandbox Code Playgroud)