XAML更改svg

时间:2019-02-27 16:05:55

标签: c# xaml uwp uwp-xaml

我正在尝试实现一个从OWM(OpenWeatherMap)请求天气数据并显示温度以及适合外部天气的天气图标的应用程序。 OWM为配件图标提供图标ID。然后,您可以通过特定的Web路径访问这些图标。在我的应用中显示这些图标效果很好。由于OWM提供的图标分辨率较低,因此我决定使用矢量图,并将其保存在应用程序目录中。使用此代码直接显示这些矢量图形

<Image Source="Assets/WeatherIcons/01d.svg" Height="300" HorizontalAlignment="Right"/>

工作正常。但是我想根据提供的OWM图标ID来调整使用的源。 目前,我的代码如下:
MainPage.xaml:

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

    <Grid Background="Black"
          PointerPressed="WakeUp">

        <StackPanel x:Name="WeatherInformation"
                    Visibility="Collapsed">

            <Image x:Name="WeatherIcon"
                   Height="300"
                   HorizontalAlignment="Right"/>

            <TextBlock Foreground="White"
                       TextAlignment="Center">

                <Run x:Name="Temperature" FontSize="90"/>
                <LineBreak/>
                <Run x:Name="WeatherDescription" FontSize="50"/>

            </TextBlock>

        </StackPanel>
    </Grid>
</Page>

MainPage.xaml.cs:

using System;
using System.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Imaging;

namespace starting
{
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer modeTimer = new DispatcherTimer();

        public MainPage()
        {
            this.InitializeComponent();
            updateWeather(this, this);

            modeTimer.Tick += new EventHandler<object>(Sleep);
            modeTimer.Interval = new TimeSpan(0, 1, 0);

            DispatcherTimer updateWeatherTimer = new DispatcherTimer();
            updateWeatherTimer.Tick += new EventHandler<object>(updateWeather);
            updateWeatherTimer.Interval= new TimeSpan(0, 30, 0);
            updateWeatherTimer.Start();
        }

        private void updateWeather(object sender, object e)
        {
            WeatherApp.WeatherAPI myWeatherApi = new WeatherApp.WeatherAPI("Friedrichshafen,de");
            OpenWeatherMapType.WeatherStream data = myWeatherApi.GetForecast();
            WeatherIcon.Source = new BitmapImage(new Uri("ms-appx:///Assets/WeatherIcons/" + data.Weather[0].Icon + ".svg"));
            Temperature.Text = Math.Round(data.Main.Temp).ToString() + "°C";
            WeatherDescription.Text = data.Weather[0].Description;
        }

        private void Sleep(object sender, object e)
        {
            StandByTime.Visibility = Visibility.Visible;
            ActiveTime.Visibility = Visibility.Collapsed;
            WeatherInformation.Visibility = Visibility.Collapsed;
            modeTimer.Stop();
        }

        private void WakeUp(object sender, PointerRoutedEventArgs e)
        {
            StandByTime.Visibility = Visibility.Collapsed;
            ActiveTime.Visibility = Visibility.Visible;
            WeatherInformation.Visibility = Visibility.Visible;
            modeTimer.Start();
        }
    }
}

启动应用程序时,没有显示天气图标的迹象。那怎么可能?

1 个答案:

答案 0 :(得分:-2)

似乎工作正常:
MainPage.xaml:

<Page>
    <Grid>
        <Image Height="300">
            <Image.Source>
                <SvgImageSource x:Name="WeatherIconSource"/>
            </Image.Source>
        </Image>
    </Grid>
</Page>

MainPage.xaml.cs:

private void updateWeather(object sender, object e)
{
    WeatherApp.WeatherAPI myWeatherApi = new WeatherApp.WeatherAPI("Friedrichshafen,de");
    OpenWeatherMapType.WeatherStream data = myWeatherApi.GetForecast();
    WeatherIconSource.UriSource = new Uri("ms-appx:///Assets/WeatherIcons/" + data.Weather[0].Icon + ".svg");
}