- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试实现一个在某些项目的 ListView 和平铺 View 之间切换的按钮。两者背后的代码是相同的,唯一的区别是显示内容的大小和属性。目前我已经实现了 ListView ,XAML 如下所示:
<StackLayout>
<ListView x:Name="FilesListView"
ItemsSource="{Binding Files}"
ItemSelected="FileItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding IsFolder, Converter={StaticResource fileTypeToImageConverter}}" WidthRequest="40" HeightRequest="40" HorizontalOptions="Start"/>
<Label Grid.Column="1" Text="{Binding Path, Converter={StaticResource pathToFilenameConverter}}" HorizontalOptions="StartAndExpand"/>
<Label Grid.Column="2" Text="{Binding ModifiedDate, Converter={StaticResource dateConverter}}" HorizontalOptions="EndAndExpand"/>
<Label Grid.Column="3" Text="{Binding Size, Converter={StaticResource fileSizeConverter}}" HorizontalOptions="EndAndExpand"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
这是局部 View ; FileBrowser 控件嵌套在另一个包含切换按钮的堆栈布局中。该按钮在 XAML 中定义如下:
<ContentPage.ToolbarItems>
<ToolbarItem Text="Toggle View" Order="Primary" Icon="{Binding ViewIcon}" Command="{Binding ToggleViewCommand}"/>
</ContentPage.ToolbarItems>
ToggleViewCommand 在哪里:
protected void ToggleView()
{
FileBrowserControl.ToggleView();
ViewModel.ToggleViewIcon();
}
我需要帮助来弄清楚如何实现 FileBrowserControl.ToggleView();
。我需要用不同的 XAML 片段(代表平铺 View )动态换出网格 XAML,或者更新代码中的网格。第一个选项在定义平铺 View 的外观方面似乎更容易,但我不想通过创建 FileBrowserXaml.xaml/FileBrowserXaml.xaml.cs 来复制逻辑背后的代码,它与 FileBrowser 具有相同的逻辑背后代码.xaml.cs。对于我正在尝试做的事情,Xamarin 最佳实践是什么?
(附带说明,这需要跨平台。)
更新:我决定尝试用代码构建数据模板。到目前为止我有这个:
public void ToggleView()
{
DataTemplate itemTemplate;
if (_view == FileBrowserView.List)
{
itemTemplate = new DataTemplate(() =>
{
var cell = new ViewCell();
var grid = new Grid();
// Define columns
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
// Define rows
var image = new Image
{
WidthRequest = 40,
HeightRequest = 40,
HorizontalOptions = LayoutOptions.Start
};
image.SetBinding(Image.SourceProperty,
new Binding("IsFolder", BindingMode.Default, new FileTypeToImageValueConverter()));
grid.Children.Add(image, 0, 0);
var pathLabel = new Label {HorizontalOptions = LayoutOptions.StartAndExpand};
pathLabel.SetBinding(Label.TextProperty,
new Binding("Path", BindingMode.Default, new PathToFilenameValueConverter()));
grid.Children.Add(pathLabel, 1, 0);
var dateLabel = new Label { HorizontalOptions = LayoutOptions.StartAndExpand };
dateLabel.SetBinding(Label.TextProperty,
new Binding("ModifiedDate", BindingMode.Default, new DateTimeDisplayValueConverter()));
grid.Children.Add(dateLabel, 2, 0);
var sizeLabel = new Label { HorizontalOptions = LayoutOptions.EndAndExpand };
dateLabel.SetBinding(Label.TextProperty,
new Binding("Size", BindingMode.Default, new FileSizeDisplayValueConverter()));
grid.Children.Add(sizeLabel, 3, 0);
cell.View = grid;
return cell;
});
}
else
{
itemTemplate = new DataTemplate(() =>
{
var cell = new ViewCell();
var grid = new Grid();
// Define columns
grid.ColumnDefinitions.Add(new ColumnDefinition {Width = new GridLength(1, GridUnitType.Star)});
grid.ColumnDefinitions.Add(new ColumnDefinition {Width = new GridLength(1, GridUnitType.Star)});
// Define rows
var image = new Image
{
WidthRequest = 80,
HeightRequest = 80,
HorizontalOptions = LayoutOptions.Start
};
image.SetBinding(Image.SourceProperty,
new Binding("IsFolder", BindingMode.Default, new FileTypeToImageValueConverter()));
grid.Children.Add(image, 0, 0);
var pathLabel = new Label { HorizontalOptions = LayoutOptions.Start };
pathLabel.SetBinding(Label.TextProperty,
new Binding("Path", BindingMode.Default, new PathToFilenameValueConverter()));
grid.Children.Add(pathLabel, 0, 1);
cell.View = grid;
return cell;
});
}
FilesListView.ItemTemplate = itemTemplate;
}
我现在遇到的问题是,在平铺 View 中,每个项目都在其自己的行中。我希望它们环绕(路径应该在图像下方,但每个路径/图像组合应该与下一个并排,就像在 Windows 资源管理器或典型的应用程序启动器中一样)。构建数据模板以实现此目的的最佳方法是什么?网格是可以使用的正确模板还是有更好的模板?
最佳答案
您尝试过使用 ResourceDictionary 和 StaticResource 吗?
我认为您可以通过将 ListView ItemTemplate 设置为 StaticResource 然后使用切换按钮切换用于 View 的按钮来完成您想要的。
下面是我的想法:
<ContentPage>
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="TileViewTemplate">
<ViewCell>
<ViewCell.View>
<!-- the layout of your template here -->
</ViewCell.View>
</ViewCell>
</DataTemplate>
<DataTemplate x:Key="ListViewTemplate">
<ViewCell>
<ViewCell.View>
<!-- the layout of your template here -->
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ListView x:Name="FilesListView" ItemsSource="{Binding Files}"
ItemsSelected="FileItemSelected"
ItemTemplate="{StaticResource TileViewTemplate}"/>
</ContentPage>
然后,toggleview 命令可以切换 ItemTemplate 属性。这样您就不必担心两个 ListViews - 它可以使 XAML 更易于管理。
只是一个想法,但 ResourceDictionary 确实对我很有帮助。
关于c# - 在 XAML/Xamarin 中交换 ListView 网格(两个网格版本,单个代码隐藏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31679717/
我已经从github https://github.com/xamarin/xamarin-forms-samples下载了Xamarin.Form示例项目 打开任何示例项目后,它不允许我在iOS S
我收到此错误: "MyApp\App.cs(7,7): Error CS0246: The type or namespace name 'Xamarin' could not be found (a
我想知道 Xamarin 是否带有 Mono 运行时及其所有应用程序包。在这种情况下,如果两个基于 Xamarin 的应用程序安装在一个设备上,该设备将拥有两个 Mono 运行时权利。这是 Xamar
如何将库导入 Xamarin? 例如,我将如何导入 json.net为我的项目使用 xamarin? 谢谢 最佳答案 Json.NET可免费获得精美包装 Xamarin-compatible Comp
我不知道如何在输入框中置顶占位符文本。 我有一个很大的输入框,想把占位符文本放在顶部。 最佳答案 您需要为每个平台创建一个自定义渲染器以对齐占位符,如下所示: public class Placeh
我很难找到有关Xamarin.Forms的后台任务支持的文档。 Xamarin.Forms是否提供对定期后台任务的支持? 我需要为Windows Phone 10和Android都实现此功能。 最佳答
Xamarin.iOS中是否提供iOS Picker?我进行了详尽的搜索,但是没有示例,也没有信息可查。但是,它在Xamarin.Form中可用。 最佳答案 UIPickerView的真实示例示例:(
有谁知道是否可以使用 Xamarin.Forms 创建CardView样式(可滚动)列表?我们需要它在iOS和Android上将呈现为相同的。还需要调整阴影等属性(略微提高每张卡) 最佳答案 这是一个
所以,我对 Xamarin 有点陌生,我试图弄清楚如何显示一个包含用户文本输入字段的弹出窗口。 DisplayAlert 不这样做,因为它没有文本输入字段。我应该使用什么? 最佳答案 您可以使用 Di
我有一个运行良好的表单应用程序,但我注意到当页面出现时,背景颜色在几分之一秒内设置不正确。 我有这个代码用于我的 OnAppearing protected override async vo
您好,我正在开发一个具有登录功能的应用程序,它可以选择让您保持登录状态,即使您关闭该应用程序也是如此。 问题是什么?这就是我在 App.cs 中所做的: var statusLog = Appli
由于BackgroundImage是一个字符串,您应该如何设置Page的背景图像?我将不胜感激任何建议。 到目前为止,我已经尝试过: MainPage = new ContentPage {
如何使用 Renderer 在 Xamarin Forms 中使用渐变效果创建此按钮? 最佳答案 在 xamarin 中,您不能将渐变颜色添加为内置功能。您必须创建不同的渲染功能。这个 link 将指
背景:我正在处理一个 C# 项目。过去,当我做 System.Console.WriteLine("Hello"); 我会看到一个弹出控制台打印“你好”。控制台今天消失了,我该怎么做才能让它再次出现?
我们每天都在使用 Xamarin 和 Xamarin Forms,并且经常遇到异常而没有任何关于如何调试的有用信息。 有时它是我们的目标,有时是 Xamarin 中的错误,尤其是 Xamarin Fo
我正在使用 xamarin studio(带有 nuget 包管理插件),并且在我的项目中有一些 nuget 包。 项目上下文菜单中有“管理”和“恢复 nuget 包”,但也有控制台吗? 最佳答案 X
我有一个 CustomCalendar 元素,它是通过扩展 ContentView 并在另一个 ContentPage 中使用此自定义 View 而创建的。我尝试使用非聚焦事件来检测外部点击。但是问题
因此,对于整个MVVM,我还是一个新手。我几乎了解它的基本知识。我有一个可以按原样工作的示例,但是我试图将其更改为MVVM样式。我只是尝试不同的例子,所以我可以学习。 (LoginPage.xaml)
我正在尝试使我的Xamarin项目在Prism和DryIoc中使用MVVM。 我主要想使用自动注册,如下所示: [AutoRegisterForNavigation] ... protected ov
我有一个问题,如何在 Forms Xamarin 中制作模态屏幕,如附加的图像。 我想知道你们是否可以向我发送一段代码或示例以了解如何做到这一点。 https://extravios.com.br/c
我是一名优秀的程序员,十分优秀!