- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试创建一个 UWP 益智游戏,我想将图片切割成 n 个部分,然后在网格中显示这些部分。
我的问题是,如何强制使用某种 NxN 样式。现在我必须最大化窗 Eloquent 能看到 3x3 的网格,如果我缩小任一侧,它将收敛到一个 2 列、1 列的网格。有办法处理吗?
这就是我所做的,我知道 RowDefinition 现在是手动的,直到我找到更好的方法。
<UserControl
x:Class="PictureSplitter.Views.PictureView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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:DesignHeight="300"
d:DesignWidth="400">
<GridView ItemsSource="{Binding Splitter.PuzzlePositions}">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="2">
<Grid x:Name="picGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding Piece.ImageSource}" />
</Grid>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</UserControl>
最佳答案
可能有几种方法可以做到这一点,这是另一种方法。我修改了 UserControl,以便在页面大小更改和/或集合更改时自动调整项目大小以将它们显示为正方形网格。
用户控件 XAML 代码:
<UserControl
x:Class="MyControls.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyControls"
Name="myControl">
<GridView Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" ItemsSource="{Binding ElementName=myControl, Path=Items}"
Width="{Binding ElementName=myControl, Path=CurrentWidth}" HorizontalAlignment="Center"
Height="{Binding Width, RelativeSource={RelativeSource Self}}">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="Margin" Value="0"/>
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemTemplate>
<DataTemplate>
<Border Padding="10" Width="{Binding ElementName=myControl, Path=ElementSize}" Height="{Binding ElementName=Width, RelativeSource={RelativeSource Self}}">
<Border BorderBrush="Red" BorderThickness="3">
<Image Source="ms-appx:///Assets/StoreLogo.png" Stretch="UniformToFill"/>
</Border>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</UserControl>
背后的UserControl代码:
public sealed partial class MyUserControl : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public IList Items
{
get { return (IList)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(IList), typeof(MyUserControl),
new PropertyMetadata(0, (s, e) =>
{
if (Math.Sqrt((e.NewValue as IList).Count) % 1 != 0)
Debug.WriteLine("Bad Collection");
}));
public void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (Math.Sqrt(Items.Count) % 1 != 0) Debug.WriteLine("Bad Collection");
RaiseProperty(nameof(ElementSize));
}
private double currentWidth;
public double CurrentWidth
{
get { return currentWidth; }
set { currentWidth = value; RaiseProperty(nameof(CurrentWidth)); RaiseProperty(nameof(ElementSize)); }
}
public double ElementSize => (int)(currentWidth / (int)Math.Sqrt(Items.Count)) - 1;
public MyUserControl()
{
this.InitializeComponent();
}
}
主页 XAML:
<Grid>
<local:MyUserControl x:Name="myControl" Items="{Binding MyItems}"/>
<Button Content="Add" Click="Button_Click"/>
</Grid>
MainPage 背后的代码:
public sealed partial class MainPage : Page
{
private ObservableCollection<int> myItems = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
public ObservableCollection<int> MyItems
{
get { return myItems; }
set { myItems = value; }
}
public MainPage()
{
this.InitializeComponent();
DataContext = this;
MyItems.CollectionChanged += myControl.Items_CollectionChanged;
}
protected override Size MeasureOverride(Size availableSize)
{
myControl.CurrentWidth = Math.Min(availableSize.Height, availableSize.Width);
return base.MeasureOverride(availableSize);
}
private void Button_Click(object sender, RoutedEventArgs e) => MyItems.Add(3);
}
程序以“Bad Collection”开始 - 有 8 个项目,所以你不能用它们制作一个正方形网格,但只要你点击提供的按钮 - 集合的计数就会变为 9,网格应该更新本身。
关于c# - 在 UWP Xaml 中创建和填充 NxN 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589510/
我想创建一个包含一堆其他 XAML 文件的主 XAML 文件。这可能吗? 最佳答案 在您的主 Xaml 中,使用 项目控件 balise 和 Xaml 组合器,您必须使用 定义您的项目用户控件 代替
如何在 xaml 中更改 ToggleSwitch 中实际开关的大小?我可以更改 OnContent 和 OffContent 的文本大小,但滑动开关永远不会更改大小。 这些都不会改变开关本身的大小,
我有一个文本 block 列表,其中可能包括里面的 url,比如: 构建失败,请在此处查看更多信息:http://... 构建成功 应用 http://myapp/无法启动,请在此处查看更多信息:ht
我想做的事 我最近一直在探索 XAML 资源字典。它们非常强大,但为了减少(甚至进一步)为适应任何修改而需要进行的更改,我想使用一些基本的算术运算来更改 HeightRequest Entry 的属性
我使用以下代码创建了一个弹出窗口,但我不知道如何将它居中 我试图在运行时自动更改边距,但我不知道该怎么做,但是有人知道如何将弹出窗口居中吗? 它没有标准维度,因为我需要全局化我的程序
哪种类型的命名更适合在 XAML 中使用: xmlns:inventoryControls="clr-namespace:Inventory.Components.Controls;assembly=
我正在尝试格式化我的 string每 3 位有逗号,如果不是整数,则为小数。我检查了大约 20 个示例,这是我最接近的示例: 但我得到一个 The property 'StringFormat' w
在 Windows Phone 8 中,我想将一个图像放在一个圆圈中。是否有类似网格的圆形容器?我知道有椭圆位它不是容器 最佳答案 这是我如何做到的。
有没有办法在 XAML 中检查逻辑非逻辑。我试图绑定(bind)到广泛使用的属性,但是对于这些新的绑定(bind),它的方式是错误的。我不想修改属性,那么有没有办法在 xaml 中反转 bool 结果
在 html 中可以说 width="20%"。这在 XAML 中当然是不允许的。有谁知道为什么会这样或者有没有办法在 XAML 中获得百分比值支持? 最佳答案 网格列定义和行定义允许比例单位(除了固
我可以动态创建 XAML 并将其弹出到我的应用程序中吗?该怎么做呢? 最佳答案 是的,这很简单: XmlReader tXml = XmlReader.Create(myXamlString); UI
简短版: 如果不使用 xamlc 预编译 xaml,应用程序中捆绑了什么以在运行时编译 xaml?它是否取决于 xamarin.forms nugets 之外的应用构建时的当前工具集? 长版: 我们有
我知道如何显示内容页面,但如何根据方向选择正确的布局。我想编写 xaml 文件:一个用于垂直方向,另一个用于水平方向。方向改变时是否可以切换xaml文件 最佳答案 Xamarin.Forms does
我是一名 Android 开发者,我是 Windows Phone 8 的新手 最近我做了一个 android 应用程序..所以我想在 Windows Phone 8 中开发具有相同 View 和相同
在库中编译 XAML 时出现以下错误: Themes\Generic.xaml(35,12): XamlCompiler error WMC0610: XBF generation error cod
我需要在几个不同的 XAML 布局文件中包含一个通用的 XAML。 我不想在其他 XAML 文件中对重复的 XAML 部分进行硬编码。 怎么做包含? 最佳答案 你能把共享代码变成UserControl
我想像这样添加一个符号: 这行不通,所以我尝试用\分隔,但也行不通。有人对我如何做到这一点有任何建议吗? 最佳答案 其实这个问题不是关于XAML的,它只是一个纯XML的问题。 XML中有些字符需要转
我在 Xaml 中定义了这样的列表。 #3599B8 #374649 #FD625E #F2C80F
在向 Microsoft 报告之前,我想在这里问一下。我有一个问题,我无法看到我的 WinUI 3 应用程序的实时可视化树。我什至看不到应用程序内工具栏。我可以在 WPF 和 UWP 应用程序中看到,
我已经定义了一个动画作为一个资源,当通过这样的 EventTrigger 调用时它可以工作: 然后
我是一名优秀的程序员,十分优秀!