.net C#一些好用的小tips
1. 打开文件夹并选中指定的文件:
Process.Start("Explorer.exe", "/select," + dev.PhotoDataFilePath);
2. 提示框显示在界面最前面:
MessageBox.Show((string)this.FindResource("PUB_STR_DOWNLOAD_FAILED"), "", MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None, MessageBoxOptions.ServiceNotification);
3. 后台代码排序前台列表:
SNRInfo_DataGrid.SetBinding(DataGrid.ItemsSourceProperty, new Binding("SNRWindowDevice.DeviceProtocol.snrCollection") { Source = this, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });

/// <summary> /// 写Log文件 /// </summary> /// <param name="logs"></param> private void WriteLogFile(string logs) { try { if (string.IsNullOrEmpty(logPath) || logPath == string.Empty) logPath = AppDomain.CurrentDomain.BaseDirectory + "App_Data" + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".log"; if (!File.Exists(logPath)) File.Create(logPath).Close(); // 创建记录log的文件,以年月日命名 using (StreamWriter file = new StreamWriter(logPath, true)) { if (!string.IsNullOrEmpty(logs)) { file.WriteLine(logs);// 直接追加文件末尾,换行 } } } catch (Exception ex) { LogHelper.ErrorLog("MainWindow.WriteLogFile", ex); } }
6. 解析多层级的json串

{{ "id": "6", "type": "1", "no": "ZTPER19110756972", "name": "吴神", "concat": "涛哥", "phone": "13811111111", "plane_sn": "ZTU3VSA1910WCAFX", "fc_sn": "4MFCC11910024G", "permit_stime": "2019-11-07", "permit_etime": "2019-11-13", "remark": "datgrid", "check_time": "", "check_status": "1", "check_remark": "", "permit_sstime": "", "permit_setime": "", "created": "2019-11-07 07:49:56", "expire_status": 2, "item": { "type": 0, "radius": 0, "height": 0, "latlon": [] } }}

string data = dataContents["data"].ToString(); JObject contents = (JObject)JsonConvert.DeserializeObject(data); CurrentNo = contents["no"].ToString(); CurrentQueryTime = selectedData.created; CurrentOrganization = contents["name"].ToString(); CurrentTel = contents["phone"].ToString(); CurrentRelieveTime = contents["permit_stime"].ToString() + "到" + contents["permit_etime"].ToString(); CurrentQueryState = contents["remark"].ToString(); if (contents["type"].ToString() == "1") CurrentFlyType = "圆形"; else CurrentFlyType = "多边形"; CurrentFlyNo = contents["plane_sn"].ToString(); CurrentFCNo = contents["fc_sn"].ToString(); CurrentResult = contents["check_status"].ToString(); CurrentRefuseReason = contents["check_remark"].ToString(); CurrentCheckTime = contents["check_time"].ToString(); CurrentStatus = contents["expire_status"].ToString(); CurrentConfirmTime = contents["permit_sstime"].ToString() + " 至 " + contents["permit_setime"].ToString(); string itemData = contents["item"].ToString(); JObject itemContents = (JObject)JsonConvert.DeserializeObject(itemData); CurrentHeightLimit = itemContents["height"].ToString(); string latLngData = itemContents["latlon"].ToString(); JArray jArray = JArray.Parse(latLngData); int index = 0; CurrentLatLng = string.Empty; foreach (var item in jArray) { if (index / 2 == 1) CurrentLatLng += "\r\n" + " " + "j" + index + ":" + item["lat"].ToString() + ", " + item["lon"].ToString(); else CurrentLatLng += " " + "j" + index + ":" + item["lat"].ToString() + ", " + item["lon"].ToString(); index++; }
7. wpf嵌入WebBrowser
a. 前端添加两个引用:
xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
b. 后台添加两个引用:
using System.Windows.Forms;
using System.Windows.Forms.Integration;
c. 创建WebBrowser,并绑定到前端

WindowsFormsHost host = new WindowsFormsHost(); System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser() { ScriptErrorsSuppressed = true, Url = new Uri("http://www.baidu.com") }; host.Child = web; this.m_grid.Children.Add(host);
8. wpf实现动画(呼吸灯效果)

1 <TextBlock Text="等待中……" FontSize="50" Foreground="Green" HorizontalAlignment="Center" FontWeight="Black" x:Name="TextBlockRecording"> 2 <TextBlock.Triggers> 3 <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 4 <BeginStoryboard> 5 <Storyboard BeginTime="0:0:0" AutoReverse="True" RepeatBehavior="Forever"> 6 <ColorAnimation From="Green" To="Transparent" RepeatBehavior="Forever" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TextBlockRecording"></ColorAnimation> 7 </Storyboard> 8 </BeginStoryboard> 9 </EventTrigger> 10 </TextBlock.Triggers> 11 </TextBlock>
9. WPF中TextBox绑定后台属性不让输入小数点怎么办?看下面:
<TextBox Grid.Row="1" Text="{Binding Path=Device.UpPrecision,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="60" FontSize="15"/>
上面这种方式在.Net4.5.2版本没法输入小数点(当然可以通过自定义样式可以输入),原因就在于UpdateSourceTrigger=PropertyChanged。这个时候可以把UpdateSourceTrigger=PropertyChanged改成UpdateSourceTrigger=Default,这样就可以输入小数点了。