【发布时间】:2018-10-14 01:34:00
【问题描述】:
我有以下 XAML 代码来创建 Windows 通用应用程序:
<Page
x:Class="CalculationQuizzer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CalculationQuizzer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Image Source="ms-appx:///Images/kingscross.jpg" Opacity=".3" Stretch="Fill"></Image>
<StackPanel VerticalAlignment="Center">
<TextBlock Text="Calculation Quizzer" TextAlignment="Center" Margin="4" FontSize="16"></TextBlock>
<TextBlock Name="questionTextBlock" Text="" TextAlignment="Center" Margin="4"></TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="4">
<TextBox Name="answerTextBox" Width="100" Margin="4" TextAlignment="Center"></TextBox>
<Button Content="Check Answer" Name="checkAnswerButton" HorizontalAlignment="Center" Margin="4" Click="checkAnswerButton_Click" ></Button>
</StackPanel>
<Button Content="Next Question" Name="getNextQuestionButton" HorizontalAlignment="Center" Margin="4" Click="getNextQuestionButton_Click" ></Button>
<TextBlock Name="resultTextBlock" Text="" TextAlignment="Center" Margin="4"></TextBlock>
</StackPanel>
<MediaElement Name="soundMediaElement"></MediaElement>
</Grid>
如您所见,Grid 使用名为kingscross.jpg 的图像作为其背景。我正在尝试这样做,以便单击按钮 Next Answer 将导致网格背景更改为名为 SKV_8915_s.jpg 的图像。
这是XAML后面的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace CalculationQuizzer
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
IQuizObject activeQuiz;
public MainPage()
{
this.InitializeComponent();
activeQuiz = new AdditionQuizObject();
questionTextBlock.Text = activeQuiz.GetQuestion();
}
private void getNextQuestionButton_Click(object sender, RoutedEventArgs e)
{
activeQuiz.NextQuestion();
questionTextBlock.Text = activeQuiz.GetQuestion();
answerTextBox.Text = "";
resultTextBlock.Text = "";
}
private void checkAnswerButton_Click(object sender, RoutedEventArgs e)
{
if (activeQuiz.CheckAnswer(answerTextBox.Text))
{
resultTextBlock.Text = "Correct! Well done.";
Uri soundsource = new Uri("ms-appx:///Sounds/right.wav");
soundMediaElement.Source = soundsource;
soundMediaElement.Play();
}
else
{
resultTextBlock.Text = "Sorry, that is not correct.";
Uri soundsource = new Uri("ms-appx:///Sounds/wrong.wav");
soundMediaElement.Source = soundsource;
soundMediaElement.Play();
}
}
}
如果你们中的一些人能告诉我,我需要将哪些代码插入到 CheckAnswerButton_Click 的事件处理程序中,以便将 Grid 的背景更改为 SKV_8915_s.jpg,那就太棒了!
【问题讨论】:
-
理论上这应该可以通过将
Grid.Background设置为ImageBrush来实现,但我自己很难让它像这个答案所说的stackoverflow.com/a/14085305/9363973 那样工作 -
嗨@SSJ5Broli,如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您找到了解决方案,并为回答者和您自己赢得了一些声誉。
标签: c# xaml uwp background imagebrush