0

MainPage.xaml にパノラマ コントロールがあり、リストピッカー コントロールを使用して、ユーザーが別のページである SettingsPage.xaml の背景の png 画像を変更できるようにしたいと考えています。リストピッカーにいくつかのテスト画像名を正常に入力し、それを使用して選択を変更できますが、SettingsPage.xaml から MainPage.xaml にアクセスして実際の画像を変更する方法がわかりません Listpicker selectedIndex変更されます。これまでのところ、私が持っているものは次のとおりです。

MainPage.xaml

<controls:Panorama x:Name="panorama" Title="Application Title">
        <controls:Panorama.Background>
            <ImageBrush ImageSource="PanoramaBackground.png"/>  //the default background
        </controls:Panorama.Background>

        ...

</controls:Panorama>

設定ページ.xaml

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme" Grid.Row="2" Grid.ColumnSpan="2"
                                        ItemTemplate="{Binding ThemeItemTemplate}" SelectedIndex="{Binding}"
                                        SelectionChanged="ThemeListPicker_SelectionChanged"/>

SettingsPage.xaml.cs

String[] Theme = 
    {
        "Default",
        "Bubbles",
        //...
    };

public SettingsPage()
    {
        InitializeComponent();

        //Theme list picker
        this.ThemeListPicker.ItemsSource = Theme;
        this.ThemeListPicker.DataContext = ThemeListPicker.SelectedIndex;
    }

private void ThemeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count <= 0) //to eliminate IndexOutOfRangeException
        {
            return;
        }

        string selectedItem = e.AddedItems[0] as string;

        switch(selectedItem)
        {
            case "Default":
                //change panorama background here (PanoramaBackground.png)
                this.ThemeListPicker.SelectedIndex = 0;
                break;
            case "Bubbles":
                //change panorama background here (PanoramaBackground_Bubbles.png)
                this.ThemeListPicker.SelectedIndex = 1;
                break;
        }
    }
4

2 に答える 2

1

分離ストレージに設定エントリを保持するために使用できる Settings.cs クラスを作成し、それらの設定の 1 つを背景画像にすることができます。

実装サンプルについては、こちらを参照してください。

http://msdn.microsoft.com/en-us/library/ff769510%28v=vs.92%29.aspx

アプリの実行中に設定が必要な場合、目的を達成するための最も簡単な方法は、App.xaml.cs 内でパブリック文字列オブジェクト (たとえば、YourString と呼ばれる) を使用することです。

次に、リスト ピッカーの選択が次のように変更されたときに、Settings.xaml ページ (実際にはコード ビハインド) から設定できます。

private void ThemeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count <= 0) //to eliminate IndexOutOfRangeException
    {
        return;
    }

    string selectedItem = e.AddedItems[0] as string;

    switch(selectedItem)
    {
        case "Default":
            //change panorama background here (PanoramaBackground.png)
            (Application.Current as App).YourString = "PanoramaBackground.png";
            break;
        case "Bubbles":
            //change panorama background here (PanoramaBackground_Bubbles.png)
            (Application.Current as App).YourString = "PanoramaBackground_Bubbles.png";
            break;
    }
}

次に、MainPage.xaml ページに移動するときに、YourString 文字列が null でないかどうかを確認します。そうでない場合は、そこから URI を作成し、パノラマ背景をその画像 URI に設定します。

于 2012-07-13T06:36:20.983 に答える
0

この問題を解決するには、MainPage.xaml で元のデフォルトの ImageSource パスを設定し、MainPage.xaml.cs の OnNavigatedTo イベントで、SettingsPage の設定に従ってパノラマの背景画像を更新しました。

MainPage.xaml

<controls:Panorama x:Name="panorama" Title="Application Title"> 
    <controls:Panorama.Background> 
        <ImageBrush ImageSource="PanoramaBackground.png"/>  //the default background 
    </controls:Panorama.Background> 

    ... 

</controls:Panorama> 

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Uri themeUri = new Uri(Settings.Theme.Value, UriKind.Relative);  //holds the uri string of the selected background from SettingsPage

        BitmapImage image = new BitmapImage(themeUri);
        ImageBrush brush = new ImageBrush();
        brush.ImageSource = image;
        panorama.Background = brush;
    }
于 2012-07-13T20:24:56.073 に答える