我想通过mainagee.xaml上的一个按钮将一个int列表(称为Buy_PlayerUNIT)传递给我的Buy单元函数.我想我可以使用命令参数.但现在我不确定了.

单位_视图_模型.cs

namespace top_down_rts
{
    public class units_view_model
    {

        public ObservableCollection<unit> Unit_list { get; set; }
        public player player_inst { get; set; }
        
        public units_view_model()
        {
            this.Unit_list = new ObservableCollection<unit>();
            this.player_inst = new player();
        }

        public ICommand buy_unit_command => new Command(buy_unit);
        void buy_unit(object parameter)
        {
       // i want this function to recieve the buy_player_unit object
            Console.WriteLine("buy");
            var values = (object[])parameter;
            int x = (int)values[0];
            int y = (int)values[1];
            int team = (int)values[2];
            int price = (int)values[3];

            Console.WriteLine(this.player_inst.money);
            this.player_inst.money -= price;
            Unit_list.Add(new unit(x, y, team));  
        }

        public object buy_player_unit()
        {
     // i want to push this into the buy_unit function
            return new object[] { 50, 50, 0, 1 };
        }


    }
}

下面是我的XML

Mainpage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             
             xmlns:local="clr-namespace:top_down_rts"
             x:Class="top_down_rts.MainPage">

    <ContentPage.BindingContext>
        <local:units_view_model/>
    </ContentPage.BindingContext>

    <StackLayout>

        <Button 
            HorizontalOptions="Start" 
            WidthRequest="200" 
            Text="buy" 
            Command="{Binding buy_unit_command}"
            CommandParameter="{Binding buy_player_unit}"
                />
    </StackLayout>
</ContentPage>

但我得到了这个错误

System.NullReferenceException: 'Object reference not set to an instance of an object.'

在这条线上

int x = (int)values[0];

所以我觉得这句话

CommandParameter="{Binding buy_player_unit}"

没有像我希望的那样传递我的对象.

为什么它不起作用?我是不是听错了什么?

推荐答案

CommandParameter需要属性,而不是方法.而您绑定的buy_player_unit值是方法buy_player_unit().

try 将其替换为属性

    public object buy_player_unit =>
        new object[] { 50, 50, 0, 1 };

或动态计算:

    public object buy_player_unit => GetPlayerUnit();

顺便问一下,为什么不能在视图模型中直接使用buy_player_unit呢?而不通过长链VM-View通过BINDING-VM VIA命令参数.

从您的示例中看,您可以删除ComandParameter,然后直接从buy_unit_command中访问buy_player_unit,而不需要参数.

Csharp相关问答推荐

在包含空项的列表上使用具有断言T的摘要表

为什么.Equals(SS,StringComparison. ClientCultureIgnoreCase)在Net 4.8和6.0之间不同?

自定义JsonEditor,用于将SON序列化为抽象类

等待限制选项似乎不适用于频道

MongoDB将JS查询转换为C#的问题

有没有办法把+02:00转换成TimeSpan?""

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

Blazor服务器端的身份验证角色

静态对象构造顺序

ASP.NET Core AutoMapper:如何解决错误 CS0121调用在以下方法或属性之间不明确

C#阻塞调用或await calling inside calling方法

如何使用自定义负载均衡器管理Ocelot负载均衡器中的多线程和批读取

Blazor Web App WASM的两个独立项目令人困惑

具有类型识别的泛型方法

Blazor Server.NET 8中的Blazore.FluentValidation问题

单元测试类型为HttpClient with Microsoft.Extensions.Http.Resilience

正在从最小API-InvocationConext.Arguments中检索参数的FromBodyAttribute

如何对特定异常使用Polly重试机制?

具有嵌套属性的IGGroup

这是T自身的布尔表达式是什么意思?