C# 将GridLayout绑定到Android时出现问题
我使用Mono技术创建游戏。在写作时,我遇到了一个问题。我需要创建与图片。我想在这里应用GridLayout或GridView。但我不知道如何在GridLayout中动态创建图像,这些图像会使用ViewModel(属性列表)进行更新。游戏中会改变物品属性列表中的方块模型,即图片的路径。如果你点击图像,它会改变C# 将GridLayout绑定到Android时出现问题,c#,mono,xamarin.android,mvvmcross,C#,Mono,Xamarin.android,Mvvmcross,我使用Mono技术创建游戏。在写作时,我遇到了一个问题。我需要创建与图片。我想在这里应用GridLayout或GridView。但我不知道如何在GridLayout中动态创建图像,这些图像会使用ViewModel(属性列表)进行更新。游戏中会改变物品属性列表中的方块模型,即图片的路径。如果你点击图像,它会改变 我也打过类似的比赛在Windows Phone中。没有问题。Android中有一个GridView可用-但我个人从未在mvvmcross中使用过数据绑定的GridView-但我听说它很
我也打过类似的比赛在Windows Phone中。没有问题。Android中有一个GridView可用-但我个人从未在mvvmcross中使用过数据绑定的GridView-但我听说它很容易添加-参见 作为栅格视图的替代方案,您可以使用垂直和水平线性布局的某种组合构建自己的栅格。您可以通过以下方式进行外部线性布局:
<Mvx.MvxBindableLinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
local:MvxItemTemplate="@layout/listitem_row"
local:MvxBind="{'ItemsSource':{'Path':'Rows'}}"
/>
其中GameRow是:
public class GameRow : MvxNotifyProperty
{
public List<GameSquare> Cells {get;private set;}
}
然后,每个ImageView显示上的绑定应该如下所示:
{'AssetImagePath':{'Path':'Icon'}}
显然,您可能不想在ViewModel中直接使用图标路径-您可能更喜欢使用枚举。如果执行此操作,则可能需要使用ValueConverter
或自定义绑定根据当前枚举值显示正确的图像
有关更多信息,请参阅中的问题和答案。现在,BindableGridView的示例已打开 以下是当文件名来自viewmodel时,我们用于在按钮上绑定图像的解决方案: 1) 创建一个绑定
public class MvxButtonIconBinding: MvxBaseAndroidTargetBinding
{
private readonly View _view;
public MvxButtonIconBinding(View view)
{
_view = view;
}
public override void SetValue(object value)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
path = Path.Combine(path, "pictures");
path = Path.Combine(path, (string)value);
if (File.Exists(path))
{
var dr = Drawable.CreateFromPath(path);
Button b = _view as Button;
var drawables = b.GetCompoundDrawables();
foreach (var d in drawables)
if (d!=null)
d.Dispose(); // To avoid "out of memory" messages
b.SetCompoundDrawablesWithIntrinsicBounds(null, dr, null, null);
}
else
{
Console.WriteLine("File {0} does not exists", path);
}
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
public override Type TargetType
{
get { return typeof(string); }
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
}
base.Dispose(isDisposing);
}
}
2) 设置绑定:
registry.RegisterFactory(new MvxCustomBindingFactory<View>("ButtonIcon", view => new MvxButtonIconBinding(view)));
registry.RegisterFactory(新的MvxCustomBindingFactory(“ButtonIcon”,视图=>新的MVxButtonNiconBinding(视图));
3) 在gridview/listview中使用它:
<ShopBazarAndroid.MvvmCross.MvxBindableGridView
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="fill_parent"
android:id="@+id/ArticleLayout"
android:columnWidth="170dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
local:MvxItemTemplate="@layout/article_buttonlayout"
local:MvxBind="{'ItemsSource':{'Path':'VisualArticles'}}" />
“物品按钮布局”:
其中“Item”是我的对象,“PictureFileName”是本地文件系统上的文件名。我是C#方面的新手,如果发现错误请宽容:)为了提供一个等价物,看看您的wp7 xaml是什么样子会很有趣。谢谢,但是如果我使用路径:“MyZooSnap.Core;component/Resources/Images/i0”,您肯定不能使用该路径!尝试将i0.png放在资产文件夹中,确保将build action设置为androidasset,然后在ViewModel中使用i0.png作为路径如果需要在不同平台上使用不同的路径,那么简单的方法是在每个平台上使用不同的值转换器。对不起,not已复制扩展:“MyZooSnap.Core;component/Resources/Images/i0.jpg”但是这种方法不起作用。。。顺便提一句我在项目UI中放置了一个图像,是吗?很难通过注释进行调试。最好的办法是尝试按照中的说明操作——这对他们很有效。如果您有问题,可以尝试在一个新问题中发布示例代码-或者尝试xamarin forumsShow me please示例路径(本地文件系统上的文件名)
public class MvxButtonIconBinding: MvxBaseAndroidTargetBinding
{
private readonly View _view;
public MvxButtonIconBinding(View view)
{
_view = view;
}
public override void SetValue(object value)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
path = Path.Combine(path, "pictures");
path = Path.Combine(path, (string)value);
if (File.Exists(path))
{
var dr = Drawable.CreateFromPath(path);
Button b = _view as Button;
var drawables = b.GetCompoundDrawables();
foreach (var d in drawables)
if (d!=null)
d.Dispose(); // To avoid "out of memory" messages
b.SetCompoundDrawablesWithIntrinsicBounds(null, dr, null, null);
}
else
{
Console.WriteLine("File {0} does not exists", path);
}
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
public override Type TargetType
{
get { return typeof(string); }
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
}
base.Dispose(isDisposing);
}
}
registry.RegisterFactory(new MvxCustomBindingFactory<View>("ButtonIcon", view => new MvxButtonIconBinding(view)));
<ShopBazarAndroid.MvvmCross.MvxBindableGridView
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="fill_parent"
android:id="@+id/ArticleLayout"
android:columnWidth="170dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
local:MvxItemTemplate="@layout/article_buttonlayout"
local:MvxBind="{'ItemsSource':{'Path':'VisualArticles'}}" />
<Button
android:id="@+id/ButtonArticle"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
local:MvxBind="{'Click':{'Path':'Command1'},'ButtonIcon':{'Path':'Item.PictureFileName'}}"
android:textSize="14dip"
android:textColor="@drawable/ToggleButtonSelector" />