在c#中为WPF中的不同菜单下载和使用不同的背景

时间:2017-06-12 21:06:39

标签: c# css wpf

好的,在我的应用程序上使用WPF,我的应用程序的背景图片我想改变另一个背景,每个不同的菜单更改

  
    

" menuComboBox"

  

目前只在

  
    

" Trainer.ContentPage"     , 在发现     的ImageSource =" /训练师;组件/资源/ content.jpg"

  

我已经规定通过令牌接收不同的菜单选项背景文件, 我可以访问:

oS.fullUrl = $"http://Mysite-testing.net/t_server/download.php?token={selectedMenu.key}";

如何在我的应用程序的WPF设置中下载并使用下载的背景图像,以及C#代码明智

<TextBlock Text="Select Menu:" Foreground="White" VerticalAlignment="Center" Grid.Column="2" Margin="16,0,0,0"/>
            <ComboBox Name="menuComboBox" Width="Auto" Margin="8,0,0,0" VerticalAlignment="Center" Grid.Column="3"
                      IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, ElementName=enableButton}" 
                      SelectionChanged="menuComboBox_SelectionChanged"/>    

1 个答案:

答案 0 :(得分:0)

正确...通过从下载文件创建位图图像,在C#中完成更多操作,如wpf部分

private void updateBackground(MenuSelect _menu)
    {
        // Check Background
        if (!String.IsNullOrEmpty(_menu.BackGround))
        {
            // Web Client Object
            using (var objClient = new WebClient())
            {
                try
                {
                    // Download Image
                    String szUrlAddress = String.Format("http://my_site.com/t_server/download.php?token={0}", _menu.BackGround);
                    byte[] imgData = objClient.DownloadData(szUrlAddress);
                    if (imgData.Length > 0)
                    {
                        using (var objStream = new MemoryStream(imgData))
                        {
                            // Create Bitmap Image
                            var myBitmapImg = new BitmapImage();
                            myBitmapImg.BeginInit();
                            myBitmapImg.StreamSource = objStream;
                            myBitmapImg.CacheOption = BitmapCacheOption.OnLoad;
                            myBitmapImg.EndInit();
                            myBitmapImg.Freeze();

                            // Create Image
                            Image myImage = new Image();
                            myImage.Source = myBitmapImg;

                            // Create Image Brush
                            ImageBrush myBrush = new ImageBrush();
                            myBrush.ImageSource = myImage.Source;

                            // Set Background
                            this.Background = myBrush;
                        }
                    }
                }
                catch (WebException ex)
                {
                    log("Background Exception: \"" + ex.Message);
                }
            }
        }
    }

private void selectMenu(MenuSelect _menu)
        {
...
...
...
 Manager.Instance.selectMenu(_menu);
 updateBackground(_menu);

 ...
 ...
 }


<TextBlock Text="Select Menu:" Foreground="White" VerticalAlignment="Center" Grid.Column="2" Margin="16,0,0,0"/>
            <ComboBox Name="menuComboBox" Width="Auto" Margin="8,0,0,0" VerticalAlignment="Center" Grid.Column="3"
                      IsEnabled="{Binding IsChecked, Converter={StaticResource InverseBooleanConverter}, ElementName=enableButton}" 
                      SelectionChanged="menuComboBox_SelectionChanged"/>