0

通过网上的深度搜索了一天,我试着直接问你...

我用 C# 编写了一个 Windows 10 UWP 应用程序。我想展示 wifi 连接的强度。我有几张图片来展示这一点。wifi 强度应显示在 TopAppBar 中。

我使用 MVVM 设置图像源。它适用于 UserControl 中的图像,但我无法在命令栏中显示图像。

我有一个事件,它给了我实际图像的 Uri(“图片”)。

    private void WiFiInformationUpdated(object sender, WiFiInformationEventArgs args)
    {
        if (args.SSID != _viewModel.WifiInformationData.SSID)
        {
            _viewModel.WifiInformationData.SSID = args.SSID;
        }

        if (args.SignalBars != _viewModel.WifiInformationData.SignalBars)
        {
            _viewModel.WifiInformationData.SignalBars = args.SignalBars;
        }

        if(args.Picture != null)
        {
            Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() =>
            {
                var pic = new BitmapImage(args.Picture);
                _viewModel.WifiInformationData.Picture = pic;
            }
            );
        }
    }

这是它在 UserControl 中正常工作的地方:

<UserControl.DataContext>
    <Binding Path="Main" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
<Image Source="{Binding WifiInformationData.Picture}" Grid.Row="1" Grid.Column="2" DataContextChanged="Image_DataContextChanged"/>

这是有问题的场景:

<Page.DataContext>
    <Binding Path="Main" Source="{StaticResource Locator}"/>
</Page.DataContext>
<Page.TopAppBar>
    <CommandBar HorizontalContentAlignment="Center" IsOpen="True" IsSticky="True" CompositeMode="Inherit">
        <CommandBar.ContentTemplate>
            <DataTemplate>
                <RelativePanel VerticalAlignment="Stretch" Width="200" >
                    <Image Source="{Binding Main.WifiInformationData.Picture, Source={StaticResource Locator}}" Width="20" RelativePanel.Below="tbPercentWifi" DataContextChanged="Image_DataContextChanged"/>
                </RelativePanel>
            </DataTemplate>
        </CommandBar.ContentTemplate>                        
    </CommandBar>
</Page.TopAppBar>

请问有什么建议吗?

4

1 回答 1

0

解决方案是:

DataTemplate 访问问题...由以下人员修复:

public static class DataTemplateObjects
{
    public static DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name == ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }
}

然后在我的事件中设置图像的来源:

    private void WiFiInformationUpdated(object sender, WiFiInformationEventArgs args)
    {
        if (args.SSID != _viewModel.WifiInformationData.SSID)
        {
            _viewModel.WifiInformationData.SSID = args.SSID;
        }

        if (args.SignalBars != _viewModel.WifiInformationData.SignalBars)
        {
            _viewModel.WifiInformationData.SignalBars = args.SignalBars;
        }

        if(args.Picture != null)
        {
            Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() =>
            {
                var pic = new BitmapImage(args.Picture);
                _viewModel.WifiInformationData.Picture = pic;
                Image img = DataTemplateObjects.FindChildControl<Image>(commandBar, "imgWifi") as Image;
                if (img == null) return;
                img.Source = pic;
            });
        }
    }
于 2017-03-03T11:01:24.490 回答