EightBall.axaml

<Window xmlns="https://github.com/avaloniaui"
        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" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaUI.EightBall"
        Title="EightBall">
    <Grid RowDefinitions="*,auto,*">
        <Grid.Background>
            <LinearGradientBrush>
                <LinearGradientBrush.GradientStops>
                    <GradientStop Offset="0.00"  Color="Red" />
                    <GradientStop Offset="0.50" Color="Indigo" />
                    <GradientStop Offset="1.00" Color="Violet" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
        </Grid.Background>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtQuestion"
                 TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"
                 Grid.Row="0" >
            [Place question here.]
        </TextBox>
        <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"
                Click="cmdAnswer_Click"
                Grid.Row="1">
            Ask the Eight Ball
        </Button>
        <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer"
                 TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"
                 Grid.Row="2">
            [Answer will appear here.]
        </TextBox>
    </Grid>
</Window>

EightBall.axaml.cs

using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using System;
using System.Collections.Generic;
using System.Threading;

namespace AvaloniaUI;

public partial class EightBall : Window
{
    public EightBall()
    {
        InitializeComponent();
    }
    private readonly List<string> _answers = new List<string>
    {
        "是",
        "否",
        "可能",
        "毫无疑问",
        "再试一次",
        "无法预测",
        "确定",
        "不太可能",
        "当然",
        "最好别告诉你",
        "我的回答是不",
        "看起来不错",
        "迹象表明是",
        "非常可疑",
        "相信你的直觉",
        "时机未到",
        "专注再问一次",
        "不要指望它",
        "前景光明",
        "谁知道呢?"
    };

    private readonly Random _random = new Random();

    public string GetRandomAnswer(string? question)
    {
        // 模拟一些处理时间(实际应用中可能是复杂计算)
        // 注意:真实应用中不要使用Thread.Sleep,这里仅用于演示
        Thread.Sleep(TimeSpan.FromSeconds(0.5));

        // 如果问题为空,返回提示
        if (string.IsNullOrWhiteSpace(question))
            return "请先输入一个问题";

        // 返回随机答案
        return _answers[_random.Next(_answers.Count)];
    }

    private void cmdAnswer_Click(object? sender, RoutedEventArgs e)
    {
        cmdAnswer.Cursor = new Cursor(StandardCursorType.Wait);
        txtAnswer.Text = GetRandomAnswer(txtQuestion.Text);
        cmdAnswer.Cursor = Cursor.Default;
    }
}

运行效果

 

posted on 2025-07-05 15:49  dalgleish  阅读(39)  评论(0)    收藏  举报