locked
使用独立存储的图片作为panorama的背景 RRS feed

  • 问题

  • 高手们,我现在在做一个自定义皮肤的功能,但是读取独立存储的图片(1000*800)作为panorama的背景的时候,不管我怎么设置stretch属性,这个图片都只占用了480*800的屏幕,也就是我滑动panorama的时候,图片并不是延伸的,而读取资源文件中的图片的时候就是正常的,这是什么问题呢?代码如下:

    请高手帮我看看怎么解决这个问题呢?

    if ((bool)(IsolatedStorageSettings.ApplicationSettings["useSystemBG"])) AppInstance.GetAppInstance().MainImageBrush = new ImageBrush() { ImageSource = new BitmapImage(new Uri("/images/bg.jpg", UriKind.Relative)) }; else { BitmapImage bitmap = new BitmapImage(); using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isostream = iso.OpenFile("custombackground.jpg", FileMode.Open, FileAccess.Read)) { bitmap.SetSource(isostream); } } AppInstance.GetAppInstance().MainImageBrush = new ImageBrush() { ImageSource = bitmap }; }

    2013年1月15日 9:41

答案

  • 你好,

    我测试了一下,是没有问题的

    我按照vs提供的模版,新建了一个项目,在第一个Panoramaitem中加了一个button

                <phone:PanoramaItem Header="first item">
                    <!--Single line list with text wrapping-->
                    <phone:LongListSelector Margin="0,0,-22,0" ItemsSource="{Binding Items}">
                        <phone:LongListSelector.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,-6,0,12">
                                    <Button x:Name="test" Content="testing" Click="test_Click_1"/>
                                    <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
                                </StackPanel>
                            </DataTemplate>
                        </phone:LongListSelector.ItemTemplate>
                    </phone:LongListSelector>
                </phone:PanoramaItem>


    后台代码如下:

    using System;
    using System.Net;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.IO.IsolatedStorage;
    using System.Windows.Resources;
    using System.Windows.Media.Imaging;
    using System.Windows.Media;
    using System.IO;
    
    namespace PanoramaApp2
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
                this.Loaded += MainPage_Loaded;
                // Set the data context of the listbox control to the sample data
                DataContext = App.ViewModel;
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                //throw new NotImplementedException();
                String tempJPEG = "image1.jpg";
    
                // Create virtual store and file stream. Check for duplicate tempJPEG files.
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(tempJPEG))
                    {
                        myIsolatedStorage.DeleteFile(tempJPEG);
                    }
    
                    IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
    
                    StreamResourceInfo sri = null;
                    Uri uri = new Uri(tempJPEG, UriKind.Relative);
                    sri = Application.GetResourceStream(uri);
    
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(sri.Stream);
                    WriteableBitmap wb = new WriteableBitmap(bitmap);
    
                    // Encode WriteableBitmap object to a JPEG stream.
                    Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
    
                    //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                    fileStream.Close();
                }
            }
    
            // Load data for the ViewModel Items
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                if (!App.ViewModel.IsDataLoaded)
                {
                    App.ViewModel.LoadData();
                }
            }
    
            private void test_Click_1(object sender, RoutedEventArgs e)
            {
                //panorama.Background
                BitmapImage bi = new BitmapImage();
                ImageBrush ib = new ImageBrush();
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("image1.jpg", FileMode.Open, FileAccess.Read))
                    {
                        bi.SetSource(fileStream);
                        ib.ImageSource = bi;
    
                    }
                }
                //this.img.Source = bi;
                this.panorama.Background = ib;
            }
        }
    }

    允许结果如下:

    点击按钮切换背景:



    Mark Yu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年1月17日 6:00
    版主