我可以在运行时更改Silverlight的浏览器外设置吗?

Ari*_*ulp 7 c# silverlight silverlight-oob

我有一个代码库,可以在各种配置中安装OOB SL5应用程序.根据运行时的配置(由init参数选择),我想在执行安装之前更新OOB设置(标题,窗口标题).似乎唯一的方法是在编译时通过OutOfBrowserSettings.xml文件(或项目设置UI).我错过了什么,或者这只是它的方式?

Bot*_*tis 2

根据 MSDN 的说法,你可以做到这一点。

要配置现有应用程序以实现浏览器外支持而不重新编译,请将配置 XML 添加到现有 .xap 文件中的清单中。为此,请将以下示例代码中的 Deployment.OutOfBrowserSettings 属性元素复制到清单文件中,然后更新值。

作为设计器的替代方法,您可以通过填充清单模板(Properties 或 My Project 文件夹中的 AppManifest.xml)来指定浏览器外设置,如以下示例所示。默认情况下,构建使用清单模板来生成应用程序清单。但是,模板 XML 在 Visual Studio 中使用起来更简单。

来源: http ://msdn.microsoft.com/en-us/library/dd833073(v=vs.95).aspx

请记住,*.xap 文件是一个简单的 zip 文件。因此您可以将其解压、修改,然后再次压缩。自动化似乎很简单。

您还可以通过实现自己的 chrome 窗口来在运行时更改应用程序的标题(我不知道如何更改图标或描述,但我认为无论如何这都是有意义的)。对于示例实现,请遵循

要本地化标题,您必须将标题的值更改为绑定,并将其绑定到您的资源(所有代码都是上面链接中示例项目的一些自定义):

主页.xaml:

<UserControl
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Silverlight4.OOB.ChromelessWindow.Demo" x:Class="Silverlight4.OOB.ChromelessWindow.Demo.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" MinWidth="400" MinHeight="300">
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>        
    <Border BorderBrush="Black" BorderThickness="1" Margin="0,-25,0,0" Grid.Row="1"/>
    <local:ControlBar x:Name="ucControlBar" VerticalAlignment="Top" Title="{Binding Source={StaticResource resxWrapper}, Path=Title}"></local:ControlBar>
    <Border BorderBrush="Black" BorderThickness="1" CornerRadius="10" Grid.Row="1" Margin="10">
        <Border.Background>
            <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                <GradientStop Color="#FFFDC97A"/>
                <GradientStop Color="White" Offset="0.5"/>
                <GradientStop Color="#FFFDC97A" Offset="1"/>
            </LinearGradientBrush>
        </Border.Background>

    </Border>
    <ComboBox SelectionChanged="ComboBox_SelectionChanged" Grid.Row="1" Height="20">
        <ComboBoxItem Content="pl-PL" />
        <ComboBoxItem Content="en-GB" />
    </ComboBox>
    <TextBlock x:Name="txtTitle" HorizontalAlignment="Center" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="32" Text="Silverlight 4 Custom Out-Of-Browser Window Demo" Margin="10" TextAlignment="Center" Foreground="#FF5A098F"/>
    <TextBlock x:Name="txtInfo" HorizontalAlignment="Center" Grid.Row="1" TextWrapping="Wrap" Text="You are running inside Browser Window" VerticalAlignment="Bottom" Margin="10" FontSize="24" TextAlignment="Center"/>        
</Grid>
Run Code Online (Sandbox Code Playgroud)

添加文件ResourceMock.cs:

 public class ResourceMock:INotifyPropertyChanged
{
    public string Title
    {
        get
        {
            MessageBox.Show(Thread.CurrentThread.CurrentCulture.Name);
            switch (Thread.CurrentThread.CurrentCulture.Name)
            {
                case "en-GB": return "English"; break;
                case "pl-PL": return "Polish"; break;
                default: return "default";
                    break;
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public void firePropertyChanged(string property)
    {
        OnPropertyChanged(property);
    }
}
Run Code Online (Sandbox Code Playgroud)

MainPage.xaml.cs:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        if (App.IsRunningOOBWithPermission)
        {
            txtInfo.Text = "You are running outside Browser Window.";
        }
        else
        {
            txtInfo.Text = "You are running inside Browser Window.";
        }
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var cultrue = (e.AddedItems[0] as ComboBoxItem).Content.ToString();
        try
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultrue);
            var res = App.Current.Resources["resxWrapper"] as ResourceMock;
            res.firePropertyChanged("Title");
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }


}
Run Code Online (Sandbox Code Playgroud)

应用程序.xaml:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         x:Class="Silverlight4.OOB.ChromelessWindow.Demo.App"
         xmlns:loc="clr-namespace:Silverlight4.OOB.ChromelessWindow.Demo"
         >
    <Application.Resources>
       <loc:ResourceMock x:Key="resxWrapper"></loc:ResourceMock>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的示例,旨在说明您可以在运行时更改标题。我假设,如果您将标题公开为可绑定属性,那么您将知道如何实现自己的本地化。

为了使上述示例正常工作,您需要将“en-GB;pl-PL”添加到项目文件 (*.csproj) 中的 SupportedCultrues 标记。