You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

扫描含JSON的二维码跳转后设备详情页黑屏问题求助

设备详情页跳转黑屏问题

我用这个网站多年,第一次发帖,格式不对或者问题太基础的话见谅。现在我在扫一个带指定JSON的二维码,想通过JSON里的ZtdId把完整设备数据填充到「设备详情页」。参数能正常传到新页面,但屏幕会黑屏。

二维码中的JSON格式

{"Version":"1.0.0","Id":"5ca169f7-9e7b-4861-abf5-859cdb468f6e","ZtdId":"dfb0527f-64d1-4f78-85ff-50cd6fab7e6c","PKID":"","SerialNumber":"","SequenceBlockNumber":1,"TotalBlocks":1}

问题现状

  • 从CollectionView页面跳转同一「设备详情页」完全正常
  • 把参数传到空白页或关于页,页面也能正常加载
  • 加了大量Console.WriteLine能显示要加载的设备所有字段,但详情页就是黑屏
  • 已经尝试过官方导航传参相关方案及网上多种代码组合,还加了「若SerialNumber为空则关联数据」的逻辑,问题依旧

相关代码

扫描页面代码

async void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
{
    try
    {
        var first = e.Results?.FirstOrDefault();
        if (first is not null)
        {
            Dispatcher.Dispatch(() =>
            {
                barcodeGenerator.ClearValue(BarcodeGeneratorView.ValueProperty);
                barcodeGenerator.Format = first.Format;
                barcodeGenerator.Value = first.Value;
            });
        }
        string Json = JToken.Parse(e.Results[0].Value).ToString();
        Models.Device device = JsonConvert.DeserializeObject<Models.Device>(Json);
        var navigationParameters = new Dictionary<string, object>
        {
            { "Device", device }
        };
        await Shell.Current.GoToAsync($"devicedetails", navigationParameters);
    }
    catch (Exception)
    {

    }
}

设备详情页代码

public Models.Device Device { get; private set; }

public void ApplyQueryAttributes(IDictionary<string, object> query)
{
    Device = query["Device"] as Models.Device;
    if(String.IsNullOrEmpty(Device.SerialNumber))
    {
        Models.Device device = DeviceData.Devices.FirstOrDefault(a => a.ZtdId == Device.ZtdId);
        Device = device;
    }
    OnPropertyChanged("Device");
}

设备数据示例

public static IList<Models.Device> Devices { get; private set; }

static DeviceData()
{
    Devices = new List<Models.Device>
    {
        new Models.Device
        {
            SerialNumber = "00-000001",
            Manufacturer = "Dell, Inc.",
            Model = "XPS 13 9360",
            PurchaseOrderNumber = "",
            GroupTag = "US-EST-001",
            DeploymentProfile = "Hybrid AAD",
            AddressableUserName = "Jane Doe",
            UserPrincipalName = "janedoe@contoso.com",
            ZtdId = "dfb0527f-64d1-4f78-85ff-50cd6fab7e6c",
            AzureActiveDirectoryDeviceId = Guid.NewGuid().ToString(),
            ManagedDeviceId = Guid.NewGuid().ToString()
        }
    };
}

排查方向及解决方案

  1. 修复空引用风险
    直接用query["Device"]会因为参数缺失报错,先做存在性判断:

    if (query.TryGetValue("Device", out var deviceObj) && deviceObj is Models.Device tempDevice)
    {
        Device = tempDevice;
    }
    else
    {
        await Shell.Current.GoToAsync("..");
        return;
    }
    
  2. 处理数据匹配空值
    FirstOrDefault找不到匹配项会返回null,此时Device为空导致UI黑屏,加空值判断:

    if(String.IsNullOrEmpty(Device.SerialNumber))
    {
        Models.Device device = DeviceData.Devices.FirstOrDefault(a => a.ZtdId == Device.ZtdId);
        if(device != null)
        {
            Device = device;
        }
        else
        {
            // 可添加无数据提示或返回逻辑
        }
    }
    
  3. 确认UI绑定上下文
    检查详情页XAML的BindingContext是否正确指向ViewModel,且绑定表达式(如{Binding Device.SerialNumber})无错误;确保ViewModel正确实现了INotifyPropertyChanged接口,OnPropertyChanged调用无误。

  4. 确保导航在UI线程执行
    扫描回调可能不在UI线程,导航操作也需调度到UI线程:

    await Dispatcher.DispatchAsync(async () =>
    {
        await Shell.Current.GoToAsync($"devicedetails", navigationParameters);
    });
    
  5. 补全异常捕获日志
    空try-catch会隐藏错误,添加日志便于排查:

    catch (Exception ex)
    {
        Console.WriteLine($"导航错误:{ex.Message}\n{ex.StackTrace}");
    }
    

内容的提问来源于stack exchange,提问作者mrhls2312

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:15:34