Windows Phone 在读取网络图片之前先显示默认图片

1.新建一个控件WindowsPhoneControl1

WindowsPhoneControl1.xaml

<UserControl x:Class="DefaultImage.WindowsPhoneControl1"

    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"

    mc:Ignorable="d"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    d:DesignHeight="480" d:DesignWidth="480">



    <Grid x:Name="LayoutRoot">

        <Image Height="69" HorizontalAlignment="Left" Margin="10,10,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="76" Source="/DefaultImage;component/hotelphoto_alterimg.png" />

    </Grid>

</UserControl>

WindowsPhoneControl1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Threading;

using System.Windows.Media.Imaging;



namespace DefaultImage

{

    public partial class WindowsPhoneControl1 : UserControl

    {

        public WindowsPhoneControl1()

        {

            InitializeComponent();

            DispatcherTimer timer = new DispatcherTimer();

            timer.Tick += new EventHandler(timer_Tick);

            timer.Interval = new TimeSpan(0, 0, 0, 0, 100);

            timer.Start();

        }



        private string myImageSource;

        public string MyImageSource

        {

            get

            {

                return myImageSource;

            }

            set

            {

                myImageSource = value;

                WebClient wc = new WebClient();

                wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);

                wc.OpenReadAsync(new Uri(myImageSource));

            }

        }



        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

        {

            BitmapImage bitmapImage = new BitmapImage();

            bitmapImage.SetSource(e.Result);

            image1.Source = bitmapImage;

        }



        void timer_Tick(object sender, EventArgs e)

        {

            MyImageSource = this.DataContext.ToString();

        }

    }

}

MainPage.xaml

<phone:PhoneApplicationPage 

    x:Class="DefaultImage.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True" xmlns:my="clr-namespace:DefaultImage">



    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>



        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        </StackPanel>



        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <ListBox Height="473" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="10,10,0,0" Name="listBox1" VerticalAlignment="Top" Width="440">

                <ListBox.ItemTemplate>

                    <DataTemplate>

                        <my:WindowsPhoneControl1 DataContext="{Binding MySource}"/>

                    </DataTemplate>

                </ListBox.ItemTemplate>

            </ListBox>

        </Grid>

    </Grid>

</phone:PhoneApplicationPage> 

MainPage.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;





namespace DefaultImage

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

            var list = new List<MyImage> { 

                new MyImage(){ MySource= "http://i.microsoft.com/global/ImageStore/PublishingImages/Asset/thumbnails/icon_monthsecurityupdates_40x40.png"},

                 //new MyImage(){ MySource= "http://i.microsoft.com/global/ImageStore/PublishingImages/Asset/thumbnails/icon_mssecurityessentials_40x40.png"}

            };

            listBox1.DataContext = list;

        }

    }

    public class MyImage

    {

        public string MySource { get; set; }

    }



}


 

 

你可能感兴趣的:(windows phone)