xaml 相关问题

可扩展应用程序标记语言(XAML)是一种基于XML的声明式语言,用于在各种框架中初始化结构化值和对象。当问题是关于具有特定框架的XAML的使用时,还应该提供框架的标签,例如, [wpf](Windows Presentation Foundation),[silverlight],[windows-phone],[windows-store-apps](Windows 8商店应用),[win-universal-app],[xamarin.forms]或[工作流程 - 基础]

如何从代码隐藏调用故事板动作?

我是 Silverlight 新手。我有一个从网上翻转的动画,我也遵循了相同的步骤。它对我来说效果很好,并且正在尝试从代码隐藏中实现它。我怎样才能做到这一点? xmlns:

回答 2 投票 0

如何在WPF中折叠星形大小的网格列?

通过将宽度设置为自动来获得具有相同宽度的三列。 通过将 Width 设置为 Auto 来获得具有相同宽度的三列。 <Grid x:Name="myGrid"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0">One</Label> <Label Grid.Row="0" Grid.Column="1" x:Name="label1">Two</Label> <Label Grid.Row="0" Grid.Column="2">Three</Label> </Grid> 我想实现的是,如果中间列折叠或隐藏,则其他两列占用剩余空间并获得相等的宽度。 如果我只是因为宽度=“*”而将可见性设置为折叠或隐藏,则其他两列宽度保持不变。 <Label Grid.Row="0" Grid.Column="1" Visibility="Collapsed">Two</Label> 我通过以编程方式将第二列宽度设置为自动来实现所需的功能,但我正在寻找其他一些解决方案(最好是 xaml 方式之一)。 private void Button_Click(object sender, RoutedEventArgs e) { this.myGrid.ColumnDefinitions[1].Width = GridLength.Auto; this.label1.Visibility = Visibility.Collapsed; } 可以使用 UniformGrid 来实现所需的行为。 确保将行数设置为 1。 <UniformGrid Rows="1"> <Label>One</Label> <Label>Two</Label> <Label>Three</Label> </UniformGrid> 其余元素均匀分布。 <UniformGrid Rows="1"> <Label>One</Label> <Label Visibility="Collapsed">Two</Label> <Label>Three</Label> </UniformGrid> 我按照 Rob 的建议添加了 Xaml Binding,如下所示: <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="{Binding MiddleColumnWidth}" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0">One</Label> <Label Grid.Row="0" Grid.Column="1" Visibility="{Binding IsSecondLabelVisible}">Two</Label> <Label Grid.Row="0" Grid.Column="2">Three</Label> xaml 背后的代码: private bool ShowOnlyTwoColumns; private GridLength MiddleColumnWidth { get { if (ShowOnlyTwoColumns) return GridLength.Auto; // Auto collapses the grid column when label is collapsed return new GridLength(1, GridUnitType.Star); } } private Visibility IsSecondLabelVisible { get { return this.ShowOnlyTwoColumns ? Visibility.Collapsed : Visibility.Visible; } } 我迟到了,但我想展示我的解决方案,以防它对任何人有用。 我用这个转换器 public class BooleanToAutoOrStarGridLengthConverter : MarkupExtension, IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is bool isStar) return new GridLength(1, isStar ? GridUnitType.Star : GridUnitType.Auto); return new GridLength(0, GridUnitType.Star); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is GridLength gridLength && gridLength.GridUnitType == GridUnitType.Star) return true; return false; } public override object ProvideValue(IServiceProvider serviceProvider) => this; } 在 XAML 中像这样: <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="{Binding IsSecondLabelVisible, Converter={BooleanToAutoOrStarGridLengthConverter}}" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0">One</Label> <Label Grid.Row="0" Grid.Column="1" Visibility="{Binding IsSecondLabelVisible}">Two</Label> <Label Grid.Row="0" Grid.Column="2">Three</Label> 我最终得到了类似于h.m.i.13答案的想法。我创建了一个通用转换器,它隐藏列/行或使用特定的网格长度值显示它。 优点是它可以处理更复杂的网格,其中并非每行都有简单的一星长度。 public class BooleanToGridLengthConverter : IValueConverter { private readonly static GridLength DefaultLength = new GridLength(0, GridUnitType.Pixel); public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is not bool booleanValue || parameter is not string stringParameter) { return DefaultLength; } if (booleanValue && TryParseGridLength(stringParameter, out var gridLength)) { return gridLength; } return DefaultLength; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } private static bool TryParseGridLength(string stringValue, out GridLength gridLength) { gridLength = DefaultLength; if (stringValue.Equals("Auto", StringComparison.OrdinalIgnoreCase)) { gridLength = GridLength.Auto; return true; } if (stringValue.EndsWith("*")) { double starCount = 1; if (stringValue.Length > 1 && !double.TryParse(stringValue.AsSpan(0, stringValue.Length - 1), out starCount)) { return false; } gridLength = new GridLength(starCount, GridUnitType.Star); return true; } if (double.TryParse(stringValue, out var pixelValue)) { gridLength = new GridLength(pixelValue, GridUnitType.Pixel); return true; } return false; } }

回答 4 投票 0

SearchBar 数据与源的绑定不起作用

我在 .NET MAUI 项目中有一个 searchBar,并且其 SearchCommand 存在问题。我将此 SearchCommand 绑定到“ClientsViewMode”中的方法,该方法与绑定 cont 不同...

回答 1 投票 0

鼠标悬停问题导致选项卡标题背景颜色发生变化

我是XAML新手,我正在设计一个包含嵌套选项卡控件的应用程序。最近我搜索了通过鼠标悬停和选定触发器来更改选项卡标题(选定=绿色,鼠标悬停=蓝色),...

回答 1 投票 0

ListView 网格中的网格

我有一个包含 id、名称和价格的列表视图。每个这样的项目都有一个带有类别的子列表。 所以“对于每个”项目我想显示所有子项目。 它应该是这样的: 但我不知道...

回答 3 投票 0

是否可以在 WPF / Touch 应用程序中弹出一个忽略 MenuDropAlignment 的窗口?

作为一点背景知识 - Windows 有一个针对触摸/平板电脑的功能,它可以根据您的“惯用手”移动弹出窗口/菜单的位置(以防止菜单出现在您的手下)。

回答 6 投票 0

{CustomResource}

在 C++ WinUI3 应用程序中进行数据绑定工作时(感谢 SimonMourier 的演示,我学到了一些东西),我还在研究 {CustomResource} 数据绑定方法。 我创建了 WinUI 打包专业版...

回答 1 投票 0

背景颜色未正确应用

我有这个自定义控件。 公共类面板:ContentView { 公共静态只读 BindableProperty CaptionProperty = BindableProperty.CreateAttached(nameof(Caption), typeof(string),

回答 2 投票 0

在 WPF C# 中根据数据网格中的另一个组合框过滤 DataGrid 中的组合框?

在我的示例中,我在数据网格中有3个组合框,分别显示国家、省份和地区。我如何以这种方式按国家过滤省份和按省份过滤地区

回答 1 投票 0

WinUI3 C++ 数据绑定

在提到我关于 TreeView 的问题时,建议我使用数据绑定来填充 TreView 的项目。 我在 Desktop C++ VS 2022 项目中创建了一个新的黑色应用程序、打包的 WinUI3。 这...

回答 1 投票 0

使用WPF + XAML Syncfusion.SfGrid.WPF组合表格中单元格的问题

我有一个问题3天都无法处理。听起来很简单,但事实上却复杂得多。我有一个应用程序,可以从 Excel 文件中获取数据并存储它...

回答 1 投票 0

如何在 C++/WinRT (WinUI3) 中将图像从内存读取到 ImageSource?

[1] 我想在 XAML 中显示 Image 控件的图像,但该图像来自字节数组。 我应该怎么办?图像格式可以是JPG、BMP或PNG之一。 [1] 我想在 XAML 中显示 Image 控件的图像,但该图像来自字节数组。 我该怎么办?图像格式可以是 JPG、BMP 或 PNG 之一。 <Image x:Name="img"/> void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&) { unsigned long long size; // iamge data size const unsigned char* buf; // image data img().Source(???); // what should I do? } [2] 我尝试了InMemoryRandomAccessStream,但没有成功。 我知道在C#中使用MemoryStream很方便,但是如何在C++中实现它? 另一个问题是这个方法是否也适用于JPG和PNG格式的图像? void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&) { unsigned long long size; // iamge data size const unsigned char* buf; // image data winrt::Microsoft::UI::Xaml::Media::Imaging::BitmapImage bmp; winrt::Windows::Storage::Streams::InMemoryRandomAccessStream stream; // How to read data from stream? bmp.SetSource(stream); img().Source(bmp); } [3] 现在功能已经实现了,但是还有两个问题 IAsyncAction MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&) { unsigned long long size; // iamge data size const unsigned char* buf; // image data winrt::Microsoft::UI::Xaml::Media::Imaging::BitmapImage bmp; winrt::Windows::Storage::Streams::InMemoryRandomAccessStream stream; winrt::Windows::Storage::Streams::DataWriter dw(st.GetOutputStreamAt(0ULL)); dw.WriteBytes({ buf, size }); // This is very inefficient! co_await dw.StoreAsync(); dw.Close(); bmp.SetSource(stream); img().Source(bmp); stream.Close(); } 首先,我发现DataWriter::WriteBytes()确实是在复制数据。但我认为ImageSource只需要从我的buf中读取即可,不需要在读取之前将整个图像完全复制到流中。也许是类似MemoryView的东西,我这样想是不是错了? 其次,DataWriter和InMemoryRandomAccessStream的Close()应该在哪里调用? BitmapImage需要释放内存吗?这里有一个异步函数,我不知道应该写在哪里 [4] 根据IInspectable,我尝试了SHCreatMemStream并搜索了大量信息以提出另一个解决方案。目前的问题是,与方法[3]同样可行,但仍然会出现内存分配问题。 #include "Shlwapi.h" #include "shcore.h" #pragma comment(lib, "shlwapi.lib") void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&) { unsigned long long size; // iamge data size const unsigned char* buf; // image data winrt::Microsoft::UI::Xaml::Media::Imaging::BitmapImage bmp; IStream* stream{ SHCreateMemStream(buf, size) }; // Copying takes a lot of time static const GUID guidIRandomAccessStream = { 0x905a0fe1, 0xbc53, 0x11df, { 0x8c, 0x49, 0x00, 0x1e, 0x4f, 0xc6, 0x86, 0xda } }; winrt::Windows::Storage::Streams::IRandomAccessStream pRas{ }; CreateRandomAccessStreamOverStream(stream, BSOS_OPTIONS::BSOS_DEFAULT, guidIRandomAccessStream, (void**)&pRas); bmp.SetSource(stream); img().Source(bmp); } 通过性能测试,发现CreateRandomAccessStreamOverStream的时间是一致的,但是SHCreateMemStream消耗的时间与图像大小成正比。可以得出,SHCreateMemStream中创建流的过程也涉及到内存的复制。 我对Windows编程中的Stream不是特别熟悉。创建流的过程一定会涉及到内存复制吗?从内存加载图像可以避免额外的开销吗?还有没有类似MemoryStreamView的结构来替代吗? 还有一个问题,我知道新的图像肯定需要内存存储。 BitmapImage设置Stream为Source会接管Stream占用的内存吗? Image控件设置BitmapImage作为Source会接管BitmapImage占用的内存吗? 如果是这样的话,我可以接受Stream分配新的内存,否则仍然是一个效率问题。 谢谢!!!!!!! 使用Stream已经彻底解决了这个问题。如前四期所述。 至于附加问题: ★1 使用 CreatStreamOnHGlobal 而不是 SHCreatMemStream。 因为SHCreatMemStream在调用过程中会对传入的内存块参数进行另一次内存复制。 CreatStreamOnHGlobal 创建流后不执行内存复制。您可以先通过GlobalAlloc分配内存,写入所需的图像数据,然后调用CreatStreamOnHGlobal,无需任何时间开销。另外,可以将CreatStreamOnHGlobal的第二个参数设置为true,自动释放GlobalAlloc分配的内存。 通过进程内存监控,我发现对于同一个程序,使用CreatStreamOnHGlobal的速度是SHCreatMemStream的两倍,并且不会导致内存泄漏。 ★2 创建的Stream需要调用Release方法减少一个引用。 IRandomAccessStream作为局部变量,会自动析构,无需主动释放。 并且BitmapImage还管理一部分引用,这些引用在离开作用域后被释放。 通过测试COM对象上Release方法的返回值,发现整个程序结束后引用计数又回到了0

回答 1 投票 0

MAUI 8 - 如何更改弹出背景叠加颜色?

使用 MAUI 7.0,下一个代码允许更改弹出背景叠加颜色,但该代码不适用于 MAUI 8.0 使用 MAUI 7.0,下一个代码允许更改弹出窗口背景覆盖颜色,但该代码不适用于 MAUI 8.0 <maui:MauiWinUIApplication x:Class="TestMaui8.WinUI.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:maui="using:Microsoft.Maui" xmlns:local="using:TestMaui8.WinUI"> <maui:MauiWinUIApplication.Resources> <Color x:Key="SystemAltMediumColor">Transparent</Color> <SolidColorBrush x:Key="SystemControlPageBackgroundMediumAltMediumBrush" Color="{StaticResource SystemAltMediumColor}" /> <SolidColorBrush x:Key="ContentDialogBackgroundThemeBrush" Color="{StaticResource SystemAltMediumColor}" /> <SolidColorBrush x:Key="ContentDialogDimmingThemeBrush" Color="{StaticResource SystemAltMediumColor}" /> <StaticResource x:Key="ContentDialogBackground" ResourceKey="SystemControlPageBackgroundMediumAltMediumBrush" /> <StaticResource x:Key="PopupLightDismissOverlayBackground" ResourceKey="SystemControlPageBackgroundMediumAltMediumBrush" /> <SolidColorBrush x:Key="SliderBorderBrush" Color="#1A73E8" /> <Style TargetType="Slider"> <Setter Property="BorderBrush" Value="{StaticResource SliderBorderBrush}"/> </Style> </maui:MauiWinUIApplication.Resources> </maui:MauiWinUIApplication> 有人知道为什么它不起作用以及如何修复它吗? 更新添加一些图片 CommunityToolkit Popup 没有 Overlay color 功能。您可以在 CommunityToolkit GitHub 页面上提出功能请求。 是的,您发布的上述代码曾经可以工作,但它不适用于具有最新 CommunityToolkit.Maui nuget 的 .NET8。 但是如果您想更改叠加颜色,这里有一个解决方法。 <toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui" ... Color="Black" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" > //make the size to be the full screen size <ContentView WidthRequest="1920" HeightRequest="1080"> <VerticalStackLayout WidthRequest="300" HeightRequest="300" BackgroundColor="Green"> <Image Source="dotnet_bot.png"/> <Label Text="Welcome to .NET MAUI!" VerticalOptions="Center" HorizontalOptions="Center" /> </VerticalStackLayout> </ContentView> </toolkit:Popup>

回答 1 投票 0

WPF ToggleButton XAML 样式

我有两个切换按钮,我正在尝试组合它们 - 有点。所以第一个按钮根据 IsChecked 是 true 还是 false 来切换图像,但是这个按钮周围有一个边框......

回答 2 投票 0

.NET MAUI TabBar 登录/注销问题

我遇到一个问题,当用户导航到“设置”选项卡并单击注销选项时,该选项会将用户导航回登录屏幕,但当用户重新登录选项卡栏时,

回答 1 投票 0

在网格中搜索处理程序

我想知道是否可以在中拥有毛伊岛搜索处理程序。我不想将其放在工具栏中,而是希望将其功能作为输入表单的一部分,我可以在其中输入...

回答 1 投票 0

窗口风格的多个内容呈现器打破了设计时间

我有自定义基窗口类,样式(和模板)被覆盖,如果模板仅包含1个ContentPresenter(ContentSource =“Content”) - 它在设计器中显示良好,但如果我...

回答 1 投票 0

WinUI 3 嵌套资源字典?

可以嵌套多级字典吗?像这样的东西: 可以嵌套多级字典吗?像这样的东西: <?xml version="1.0" encoding="utf-8"?> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Dark"> <ResourceDictionary x:Key="Button1"> <SolidColorBrush x:Key="ButtonBackground" Color="HotPink" /> <SolidColorBrush x:Key="ButtonBackgroundDisabled" Color="HotPink" /> <SolidColorBrush x:Key="ButtonBackgroundPressed" Color="HotPink" /> <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="HotPink" /> <SolidColorBrush x:Key="ButtonForeground" Color="White" /> <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="White" /> <SolidColorBrush x:Key="ButtonBorderBrush" Color="White" /> <SolidColorBrush x:Key="ButtonBorderBrushPointerOver" Color="White" /> <SolidColorBrush x:Key="ButtonBorderBrushFocused" Color="White" /> <SolidColorBrush x:Key="ButtonBorderBrushPressed" Color="White" /> </ResourceDictionary> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> </ResourceDictionary> 是否可以以某种方式访问Button1字典? ResourceDictionary 应该添加到 MergedDictionary 中,所以你不能使用这个 <ResourceDictionary x:Key="Dark"> <ResourceDictionary x:Key="Button1"> 如果您想为按钮定义多种样式,您应该这样做: <ResourceDictionary x:Key="Light"> <Style x:Key="Button1"

回答 1 投票 0

ResourceDictionary 应用 Source 会引发异常

简单代码: ResourceDictionary dict = new ResourceDictionary(); Uri 源 = new(@"pack://application:,,,/Resources/ResourceDictionary1.xaml", UriKind.Absolute); dict.Source = 来源; T...

回答 1 投票 0

WinUI 3 按钮自定义主题

我希望我的按钮有自己的自定义颜色主题(背景、前景、边框、鼠标移入、鼠标移出、单击颜色等),所以如果我写这样的内容: 我希望我的按钮有自己的自定义颜色主题(背景、前景、边框、鼠标移入、鼠标移出、单击颜色等),所以如果我写这样的内容: <Button type="Primary">Action1</Button> <Button type="Secondary">Action2</Button> 我将有 2 个不同颜色的按钮。此外,这些按钮主题应根据主应用程序主题(浅色/深色)更改颜色。 我是 WinUI 新手,你能帮助我实现这个目标吗? 我深入研究了颜色、主题、画笔、资源、自定义控件,最终迷失了方向。 我需要创建自定义按钮吗?或者控制模板?或者仅仅利用资源就足够了?很多自定义/用户/模板控件和资源类型确实令人困惑。 您可以使用 ThemeDictionaries 为此: <Page.Resources> <ResourceDictionary> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Light"> <Style x:Key="Type1ButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button"> <Setter Property="Background" Value="Red" /> </Style> <Style x:Key="Type2ButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button"> <Setter Property="Background" Value="Blue" /> </Style> <Style x:Key="Type3ButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button"> <Setter Property="Background" Value="Green" /> </Style> </ResourceDictionary> <ResourceDictionary x:Key="Dark"> <Style x:Key="Type1ButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button"> <Setter Property="Background" Value="HotPink" /> </Style> <Style x:Key="Type2ButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button"> <Setter Property="Background" Value="LightBlue" /> </Style> <Style x:Key="Type3ButtonStyle" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button"> <Setter Property="Background" Value="LightGreen" /> </Style> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> </ResourceDictionary> </Page.Resources> <StackPanel> <Button Content="Type 1" Style="{ThemeResource Type1ButtonStyle}" /> <Button Content="Type 2" Style="{ThemeResource Type2ButtonStyle}" /> <Button Content="Type 3" Style="{ThemeResource Type3ButtonStyle}" /> </StackPanel> 更新 如果您需要覆盖以下资源: 按钮背景指针上方 按钮背景按下 等等 恐怕你不能使用样式。实现此目的的一种方法是覆盖每个 Button 上的资源,因为 我们无法使用 Style 应用资源: <Button Content="Type 1" Click="Button_Click"> <Button.Resources> <ResourceDictionary> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Light"> <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Red" /> </ResourceDictionary> <ResourceDictionary x:Key="Dark"> <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="HotPink" /> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> </ResourceDictionary> </Button.Resources> </Button> 顺便说一句,您可以在 generic.xaml 中找到 Button 的资源。

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.