WPF读取json文件,用到combox控件

通过JSON文件,读取相关的配置或者信息,并将数值传给相关控件,用于初始化

wpf 初始化 Loaded="Window_Loaded"

xml 复制代码
<Window x:Class="相机镜头选型.MainWindow" Loaded="Window_Loaded"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        xmlns:svgc = "http://sharpvectors.codeplex.com/svgc/"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:相机镜头选型"
        mc:Ignorable="d"
        Title="相机镜头选型工具" Height="500" Width="1000"
       >

读取json 给到combox

包含两个文件:1.主文件(主题程序);2.初始化文件(初值加载)

1.主文件(主题程序)

c# 复制代码
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     d1.Text = "2700";
     res_h.Text = "2046";
     vof_h.Text = "1000";
     res_w.Text = "1086";
     gq.Text = "2";
     size.Text = "0.00465";
     width.Text = "3800";
     length.Text = "12000";
     product.Text = "1000";
     speed.Text = "2000";
     cameranum.Text = "4";
     camera.SelectedIndex = 0;
     InitialValue InitV = new InitialValue();
     //List<ComboBoxItem> items = InitV.LoadData();
     camera.ItemsSource = InitV.LoadData();
     //foreach (var item in InitV.LoadData())
     //{
     //    Console.WriteLine($"DisplayText: {item.DisplayText}, Value1: {item.Value1}, Value2: {item.Value2}, Pixelsize: {item.Pixelsize}");
     //}
 }

2.初始化文件中的内容

c# 复制代码
        public List<ComboBoxItem> LoadData()
        {
            string jsonString = File.ReadAllText("D:\\02_Study\\06_WPF\\WpfApp1\\相机镜头选型\\Asset\\config.json");

            // 解析 JSON 字符串到对象
            List<ComboBoxItem> items = JsonConvert.DeserializeObject<List<ComboBoxItem>>(jsonString);

            // 使用 items 对象
            foreach (var item in items)
            {
                Console.WriteLine($"DisplayText: {item.DisplayText}, Value1: {item.Value1}, Value2: {item.Value2}, Pixelsize: {item.Pixelsize}");
            }
            return items;
        }

需要解析的json文件

json 复制代码
[
    {
        "DisplayText": "Basler ace Classic acA2000-340km 200万",
        "Value1": 2048,
        "Value2": 1088,
        "Pixelsize": 0.0055
    }
 ]
相关推荐
FuckPatience6 小时前
WPF xaml中的 xmlns:d=“http://schemas.microsoft.com/expression/blend/2008“ 的使用
ui·wpf
就是有点傻8 小时前
WPF加载动画
ui·wpf
kylezhao20198 小时前
WPF Binding 从入门到精通
wpf
数研小生19 小时前
构建命令行单词记忆工具:JSON 词库与艾宾浩斯复习算法的完美结合
算法·json
编码者卢布1 天前
【Azure Stream Analytic】用 JavaScript UDF 解决 JSON 字段被转成 Record 的关键点
javascript·json·azure
电商API&Tina1 天前
电商数据采集 API 接口 全维度解析(技术 + 商业 + 合规)
java·大数据·开发语言·数据库·人工智能·json
liwulin05061 天前
【JSON】使用com.fasterxml.jackson解析json字符串
java·数据库·json
Poetinthedusk1 天前
WPF应用跟随桌面切换
开发语言·wpf
奔跑的呱呱牛1 天前
geojson-to-kml (KML 格式转换工具)
arcgis·json
一起养小猫1 天前
Flutter for OpenHarmony 实战:打造功能完整的云笔记应用
网络·笔记·spring·flutter·json·harmonyos