Can I change Silverlight out-of-browser settings at runtime?
Asked Answered
E

1

7

I have a single codebase that can install an OOB SL5 app in various configurations. Based on the configuration at runtime (selected by init parameters) I would like to update the OOB Settings (title, window title) prior to performing the install. It appears that the only way is through the OutOfBrowserSettings.xml file (or the Project Settings UI) at compile time. Am I missing something or is this just the way it is?

Earmark answered 14/5, 2012 at 23:44 Comment(0)
S
2

According to MSDN you can do that.

To configure an existing application for out-of-browser support without recompiling, add the configuration XML to the manifest in an existing .xap file. To do this, copy the Deployment.OutOfBrowserSettings property element from the following example code into the manifest file, and then update the values.

As an alternative to the designer, you can specify the out-of-browser settings by populating the manifest template (AppManifest.xml in the Properties or My Project folder) as shown in the following example. By default, the build uses the manifest template to generate the application manifest. However, the template XML is simpler to use in Visual Studio.

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

Remember, that the *.xap file is a simple zip. So you can extract it, modify, and then zip it again. Seems pretty simple to automate.

You can also change the title of the application at runtime (i don't know how to change the icon or description, but i think that woulndt have sense anyway) by implementing your own chrome window. For sample implementation follow this

To localize title you would have to change the value of title, to binding, and bind it to your resource(all code is some customization of sample project from link above):

MainPage.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>

Added file 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);
    }
}

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());
        }
    }


}

App.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>

This is very simple sample, to show, that you can change the title at Runtime. Im assmuming, that if you have title exposed as bindable property, then you will know how to implement your own localization.

For the above sample to work, you need to add "en-GB;pl-PL" to the SupportedCultrues tag, in the project file (*.csproj).

Socioeconomic answered 18/5, 2012 at 9:46 Comment(7)
Thanks for the response. This works fine for modifying the XAP file before deploying, but remember that I need to be able to modify the current OOB settings at runtime when they install.Earmark
Ok, i get it now. Well, its not supported by default, you would have to create a custom window. Take a look hereSocioeconomic
That still wouldn't achieve what I need (though I can see maybe my question wasn't clear enough). Let me give a better example: One single XAP file can serve multiple languages by switching resource files at runtime. If the user decides to install that app OOB though, the only title, description, and icon they see will be what comes out of the manifest. I need to adapt the title that shows with the installed app to the current locale just like the application strings do.Earmark
I've edited my answer, to show an example of changing the window title on the fly.Socioeconomic
Thanks! That's still not the OOB setting though. I need the title and description that shows as the shortcut on the desktop. Thanks a lot for trying. I definitely appreciate you taking the time.Earmark
Ok, i now have the full understanding of your problem. I think that the only solution would be to create some code (on the server) to edit AppManifest.xml inside XAP.. Maybe some web service, that will give you the correct XAP depending on the OOB settings you passed to the parameters of that ws?? You could then embed the xap in html page by using javascript. But i guess implementing something like this would take a lot of time.Socioeconomic
Thanks! That's not a bad idea. I don't think it would work for us, but it's still a good approach. Given all the time you've spent helping with this, and I think I have a solid answer that it can't be done through the API, I'm awarding you the bounty. Thanks again!Earmark

© 2022 - 2024 — McMap. All rights reserved.