WPF开发中常用的一些方法汇总

2022-06-04 17:23:23 浏览数 (1)

颜色

设置背景色

代码语言:javascript复制
System.Windows.Media.Color color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#4597FF");
btnConfirm.Background = new SolidColorBrush(color);

项目配置

代码语言:javascript复制
/// <summary>
/// 从配置文件获取Value
/// </summary>
/// <param name="key">配置文件中key字符串</param>
/// <returns></returns>
public static string GetConfigValue(string key)
{
    try
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //获取AppSettings的节点 
        AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
        return appsection.Settings[key].Value;
    }
    catch
    {
        return "";
    }
}

/// <summary>
/// 设置配置文件
/// </summary>
/// <param name="key">配置文件中key字符串</param>
/// <param name="value">配置文件中value字符串</param>
/// <returns></returns>
public static bool SetConfigValue(string key, string value)
{
    try
    {
        //打开配置文件 
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //获取AppSettings的节点 
        AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
        appsection.Settings[key].Value = value;
        config.Save();

        return true;
    }
    catch
    {
        return false;
    }
}

配置文件App.config

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <!--0正式 1测试-->
    <add key="IsDebug" value="1" />
  </appSettings>
</configuration>

ListBox

注意

点击事件不要添加在Item的模板中,除非模板中有多个可点击的项。

XAML

代码语言:javascript复制
<ListBox
         x:Name="type_list_lb"
         Grid.Column="1"
         Background="#f3f3f3"
         BorderThickness="0"
         ItemContainerStyle="{StaticResource ListBoxItemContainerStyle1}"
         ItemTemplate="{StaticResource typeItemDT}"
         ItemsSource="{Binding typeList}"
         Template="{StaticResource ListBoxTemplateH}"
         SelectionChanged="type_list_lb_SelectionChanged"/>

点击事件

代码语言:javascript复制
private async void type_list_lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  object sitem = type_list_lb.SelectedItem;
  if (sitem == null)
    return;

  if (sitem is ZListItemModel)
  {
    ZListItemModel item = (ZListItemModel)sitem;

    ObservableCollection<ZListItemModel> typeList = pageData.typeList;
    for (int i = 0; i < typeList.Count; i  )
    {
      ZListItemModel item_temp = typeList[i];
      if (item_temp == item)
      {
        item_temp.selected = 1;
      }
      else
      {
        item_temp.selected = 0;
      }
    }
  }
}

如果想让选中的项能够再次点击

设置选中索引即可

代码语言:javascript复制
type_list_lb.SelectedIndex = -1;

多线程

线程切换

代码语言:javascript复制
private async Task myFunc()
{
    MyApp.Myloading.Show();
    await Task.Run(
        () =>
        {
            // 分线程耗时操作
			Thread.Sleep(1000);
        }
    );
    MyApp.Myloading.Hide();
}

延迟执行

代码语言:javascript复制
internal class ZDelayUtil
{
    public static void delay(int milliseconds, Dispatcher dispatcher, Action act)
    {
        Task.Run(() =>
                 {
                     Thread.Sleep(milliseconds);
                     dispatcher.Invoke(act);
                 });
    }
}

调用

代码语言:javascript复制
ZDelayUtil.delay(
    3000, 
    Dispatcher, 
    () =>
    {
        tip_outer.Visibility = Visibility.Hidden;
    }
);

0 人点赞