C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – Windows Phone 8.1从远程URL占位符加载图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在搞乱一些 Windows手机开发,因为我是 Android新手背景.

我正在使用“theCatAPI”来加载猫的随机图片显示它,然后当点击图片或屏幕底部的按钮时,图像会刷新到新图片.

到目前为止,我有以下内容

XAML:

<Page
    x:Class="CatFactsPics.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CatFactsPics"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid x:Name="LayoutRoot">

        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>

        <Grid.RowDeFinitions>
            <RowDeFinition Height="Auto"/>
            <RowDeFinition Height="*"/>
        </Grid.RowDeFinitions>

        <!-- TitlePanel -->
        <StackPanel Grid.Row="0" Margin="24,17,28">
            <TextBlock Text="My Application" Style="{ThemeResource TitleTextBlockStyle}" Typography.Capitals="SmallCaps"/>
            <TextBlock Text="page title" Margin="0,12,0" Style="{ThemeResource HeaderTextBlockStyle}"/>
        </StackPanel>

        <!--TODO: Content should be placed within the following grid-->
        <Grid Grid.Row="1" x:Name="ContentRoot">
            <Image HorizontalAlignment="Center" Stretch="UniformToFill" VerticalAlignment="Center" x:Name="KittyPic" Tapped="KittyPic_Tapped"/>
            <Button HorizontalAlignment="Center" VerticalAlignment="Bottom" x:Name="newPic" Click="newPic_Click" >New Kitty</Button>
        </Grid>
    </Grid>
</Page>

并在page.cs中:

...
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);

            Uri myUri = new Uri("http://thecatapi.com/api/images/get?format=src&type=jpg",UriKind.Absolute);
            KittyPic.source = new BitmapImage(myUri);
        }
...
        private void newPic_Click(object sender,RoutedEventArgs e)
        {
            Uri myUri = new Uri("http://thecatapi.com/api/images/get?format=src&type=jpg",UriKind.Absolute);
            BitmapImage bmi = new BitmapImage();
            bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            bmi.UriSource = myUri;
            KittyPic.source = bmi;
        }

我有一些问题:

1)这是正确的做事方式吗?在Android中,我会尝试异步执行操作以避免拖延UI线程.话虽如此,我似乎并没有像现在这样对待任何问题.我不熟悉Windows的做事方式,也没有找到任何资源给出任何解释或建议.

2)在新图像重新出现之前,显示新图像的延迟导致图像视图变黑的短(几秒)周期.有没有一种方法来设置它,以便旧图片保持到新的图片保持物理准备好显示,或者显示占位符“加载”图像,直到新的图像可以替换它.

关于如何做事的任何其他建议或提示都会很棒,谢谢.

解决方法

1)使用当前代码,您不会阻止UI线程,因为您在UI线程上设置了URI,但实际加载的图像是在另一个线程上自动完成的. (对于手动下载图像,字符串等,您可能会使用 async/await来避免锁定UI线程).

2)图像变黑,因为您在加载新图像之前更改了ImageSource.你提到了几种解决这个问题的方法.但大多数情况下常见的是你需要在Image控件上使用ImageOpened和ImageFailed事件,每当图像加载完成时触发(或发生错误,例如没有互联网连接).这是一个在加载时显示加载栏的示例,它只是隐藏/显示加载进度:

在page.xaml文件中:

<Grid Grid.Row="1" x:Name="ContentRoot">
    <Image x:Name="KittyPic" Tapped="KittyPic_Tapped" ImageOpened="KittyPic_ImageOpened" ImageFailed="KittyPic_ImageFailed" />
    <StackPanel x:Name="LoadingPanel" VerticalAlignment="Center">
        <ProgressBar IsIndeterminate="True" IsEnabled="True" />
        <TextBlock Text="Loading image..." HorizontalAlignment="Center" />
    </StackPanel>
</Grid>

并在page.xaml.cs文件

private void KittyPic_Tapped(object sender,TappedRoutedEventArgs e)
{
    LoadingPanel.Visibility = Visibility.Visible;
    Uri myUri = new Uri("http://thecatapi.com/api/images/get?format=src&type=jpg",UriKind.Absolute);
    BitmapImage bmi = new BitmapImage();
    bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    bmi.UriSource = myUri;
    KittyPic.source = bmi;
}

private void KittyPic_ImageOpened(object sender,RoutedEventArgs e)
{
    LoadingPanel.Visibility = Visibility.Collapsed;
}

private async void KittyPic_ImageFailed(object sender,ExceptionRoutedEventArgs e)
{
    LoadingPanel.Visibility = Visibility.Collapsed;
    await new MessageDialog("Failed to load the image").ShowAsync();
}

您可以使用TextBlock和ProgressBar来显示您想要的任何内容,或者例如在两个图像之间交换以继续显示旧图像.

对于其他建议,我认为当你习惯了基础知识时,请看一下data bindings,它非常有用且功能强大.另请看一下关于MVVM pattern的这篇文章.

大佬总结

以上是大佬教程为你收集整理的c# – Windows Phone 8.1从远程URL占位符加载图像全部内容,希望文章能够帮你解决c# – Windows Phone 8.1从远程URL占位符加载图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: