C# Avalonia 修改元件文字顏色並且想要統一使用資源方式指定 所以只能透過CSS方法指定特定CLASS

C# Avalonia 修改元件文字顏色並且想要統一使用資源方式指定 所以只能透過CSS方法指定特定CLASS

C# Avalonia 修改元件文字顏色並且想要統一使用資源方式指定 所以只能透過CSS方法指定特定CLASS


資料來源: gemini


code(cs)

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using System;
using System.Reflection.Emit;
using System.Security.Cryptography;
using VPOS;

namespace VPOS_Avalonia;

public partial class Devices : UserControl
{
    public int m_intSID = -1;
    public Devices(int SID)
    {
        InitializeComponent();
        m_intSID = SID;
        SetUI();
    }
    public event EventHandler<RoutedEventArgs> ExternalClicked;
    private void Edit_Clicked(object sender, RoutedEventArgs e)
    {
        DevicesList.m_intDeviceSID = m_intSID;
        ExternalClicked?.Invoke(this, e);//事件轉發(委派)
    }
    public void SetUI()
    {
        int index = -1;
        for (int i = 0; i < SQLDataTableModel.m_printer_valueList.Count; i++)
        {
            if (SQLDataTableModel.m_printer_valueList[i].SID == m_intSID)
            {
                index = i;
                LabTitle.Text = SQLDataTableModel.m_printer_valueList[i].printer_name;//印表機名稱
                break;
            }
        }

        if (index >= 0)
        {
            string Strprint_template = (SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_template.Length > 0) ?
                SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_template.Split(";")[0] : "";
            if (Strprint_template.Length == 0)
            {
                Strprint_template = "『無設定』或『範本被停用』";

                // 切換為警告樣式
                Lab30.Classes.Remove("deviceText");
                Lab30.Classes.Add("warningText");
            }
            else
            {
                // 切換回預設主題樣式
                Lab30.Classes.Remove("warningText");
                Lab30.Classes.Add("deviceText");
            }

            Lab30.Text = string.Format("範本名稱: {0}", Strprint_template);//範本名稱

            string StrBuf00 = string.Format("{0}",
                (SQLDataTableModel.m_printer_valueList[index].printer_config_value.new_bill_no_print == "N") ? "" : "新單訂單不列印");

            string conn_data = "";
            switch (SQLDataTableModel.m_printer_valueList[index].printer_config_value.conn_type)
            {
                case "RS232":
                    conn_data = $"{SQLDataTableModel.m_printer_valueList[index].printer_config_value.serial_port};{SQLDataTableModel.m_printer_valueList[index].printer_config_value.baud_rate.ToString()}";
                    break;
                case "TCP/IP":
                    conn_data = $"{SQLDataTableModel.m_printer_valueList[index].printer_config_value.tcp_ip_address};{SQLDataTableModel.m_printer_valueList[index].printer_config_value.tcp_port.ToString()}";
                    break;
                case "Driver":
                    conn_data = SQLDataTableModel.m_printer_valueList[index].printer_config_value.driver_name;
                    break;
            }
            string StrBuf01 = string.Format("驅動程式: {1}",
                SQLDataTableModel.m_printer_valueList[index].printer_config_value.conn_type,
                conn_data);
            Lab20.Text = StrBuf01;//連接模式

            string StrFormat02 = (SQLDataTableModel.m_printer_valueList[index].output_type != "I") ?
                                  "列印份數: {0}" :
                                  "列印份數: 1";//依照輸出類型調整顯示資訊 ~ 發票(I)列印份數固定且一定要印
            string StrBuf02 = string.Format(StrFormat02,
                SQLDataTableModel.m_printer_valueList[index].printer_config_value.bill_print_count);
            //Lab31.Text =(StrBuf00.Length>0) ? (StrBuf02 + ";" + StrBuf00) : StrBuf02;//列印份數

            string StrBuf03 = string.Format("設備狀態:{0}", (SQLDataTableModel.m_printer_valueList[index].printer_config_value.on_off_state == "Y") ? "ON" : "OFF");
            Lab11.Text = StrBuf03;//設備狀態


            Lab10.Text = "設備型號:" + SQLDataTableModel.m_printer_valueList[index].printer_config_value.device_model;//設備型號

            string StrBuf04 = "";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.new_bill_no_print == "N") ? "" : "新單不列印; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_inv_receipt == "N") ? "" : "列印發票交易明細; "; 
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_logo == "N") ? "" : "列印Logo; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.big_callnum == "N") ? "" : "取餐號加大; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_barcode == "N") ? "" : "列印條碼; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.start_buzzer == "N") ? "" : "開啟提示音; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.external_buzzer == "N") ? "" : "外接蜂鳴器; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.big_order_type == "N") ? "" : "訂單類型加大; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.big_takeaways_no == "N") ? "" : "外賣單號加大; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.big_table == "N") ? "" : "桌號加大; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.single_report == "N") ? "" : "只印簡表; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.no_print_price == "N") ? "" : "不印價格; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_ticket_memo == "N") ? "" : "列印備註; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.merge_product == "N") ? "" : "商品合併列印; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.print_product_price == "N") ? "" : "列印商品金額; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.product_single_cut == "N") ? "" : "一菜一切; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.product_big_gap == "N") ? "" : "商品間距加大; ";
            StrBuf04 += (SQLDataTableModel.m_printer_valueList[index].printer_config_value.label_bottom_info.Length > 0) ? $"底部列印資訊:{SQLDataTableModel.m_printer_valueList[index].printer_config_value.label_bottom_info}" : "";

            StrBuf04 = (StrBuf04.Length > 0) ? StrBuf04.Substring(0, StrBuf04.Length - 2) : "";
            //Lab40.Text = StrBuf04;
            Lab31.Text = (StrBuf04.Length > 0) ? (StrBuf02 + "; " + StrBuf04) : StrBuf02;//列印份數
        }

    }

}


code(axmal)

<UserControl 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="500" d:DesignHeight="345"
             x:Class="VPOS_Avalonia.Devices">
	<UserControl.Styles>
		<!-- 預設樣式(吃主題顏色) -->
		<Style Selector="TextBlock.deviceText">
			<Setter Property="Foreground" Value="{DynamicResource VPOS_DevicesTextColor}" />
		</Style>
		<!-- 警告/錯誤樣式(強制變紅) -->
		<Style Selector="TextBlock.warningText">
			<Setter Property="Foreground" Value="Red" />
		</Style>
	</UserControl.Styles>

	<Border x:Name="Frame" BorderBrush="{DynamicResource VPOS_Selectground}" Background="{DynamicResource VPOS_Foreground}" Height="220" MaxWidth="960" MinWidth="341" BorderThickness="5" CornerRadius="10" Margin="-3">
		<Grid x:Name="FullGrid" RowDefinitions="1*,0.9*,0.9*,0.9*,0.9*,0.5*,0.9*" ColumnDefinitions="1.3*,0.7*" Margin="10,10,10,10">
			<TextBlock x:Name="LabTitle" Text="印表機名稱" Foreground="{DynamicResource VPOS_DevicesTextColor}" FontSize="22" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
			<TextBlock x:Name="Lab10" IsVisible="False" Text="設備型號:" Foreground="{DynamicResource VPOS_DevicesTextColor}" FontSize="20" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>	
			<TextBlock x:Name="Lab11" Text="設備狀態:" Foreground="{DynamicResource VPOS_DevicesTextColor}" FontSize="20" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
			<TextBlock x:Name="Lab20" Text="驅動程式:" Foreground="{DynamicResource VPOS_DevicesTextColor}" FontSize="20" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
			<TextBlock x:Name="Lab30" Text="範本名稱:" Classes="deviceText" FontSize="20" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
			<TextBlock x:Name="Lab31" Text="列印份數:" Foreground="{DynamicResource VPOS_DevicesTextColor}" FontSize="20" Grid.Row="4" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="2" VerticalAlignment="Top" HorizontalAlignment="Left"/>
			<TextBlock x:Name="Lab40" IsVisible="False" Text="合取方塊狀態" Foreground="{DynamicResource VPOS_ForegroundReverse}" FontSize="16" Grid.Row="4" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="2" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left"/>
			<Image x:Name="Edit" Source="avares://VPOS/Assets/edit.png" Height="40" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" PointerPressed="Edit_Clicked"/>
		</Grid>
	</Border>
</UserControl>


重點

            if (Strprint_template.Length == 0)
            {
                Strprint_template = "『無設定』或『範本被停用』";

                // 切換為警告樣式
                Lab30.Classes.Remove("deviceText");
                Lab30.Classes.Add("warningText");
            }
            else
            {
                // 切換回預設主題樣式
                Lab30.Classes.Remove("warningText");
                Lab30.Classes.Add("deviceText");
            }
	<UserControl.Styles>
		<!-- 預設樣式(吃主題顏色) -->
		<Style Selector="TextBlock.deviceText">
			<Setter Property="Foreground" Value="{DynamicResource VPOS_DevicesTextColor}" />
		</Style>
		<!-- 警告/錯誤樣式(強制變紅) -->
		<Style Selector="TextBlock.warningText">
			<Setter Property="Foreground" Value="Red" />
		</Style>
	</UserControl.Styles>

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *