It's a cross-mobile pprogram, no Windows work. Here's the 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:PlannerApp"
x:Class="PlannerApp.MainPage">
<ContentPage.BindingContext>
<local:AssignmentViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<StackLayout>
<Entry
x:Name="InputField"
Text="{Binding NewAssignmentInputValue}"
Placeholder="Assignment Name"
ReturnCommand="{Binding AddAssignmentCommand}"
/>
<ListView x:Name="AssignmentList" ItemsSource="{Binding Assignments}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<FlexLayout JustifyContent="SpaceBetween" AlignItems="Center" Padding="20,0">
<Label Text="{Binding AssignmentName}" TextColor="Black"/>
<ImageButton
Source="delete.png"
Scale="0.5"
Command="{Binding Path=BindingContext.RemoveAssignmentCommand, Source={x:Reference Assignments}}"
CommandParameter="{Binding .}"
/>
</FlexLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
</ContentPage>
and here's the viewModel for the Assignment class:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace PlannerApp
{
public class AssignmentViewModel
{
public static void Init() { }
public ObservableCollection<Assignment> Assignments { get; set; }
/// <summary>
/// Rendering reference for the View
/// </summary>
public AssignmentViewModel()
{
Assignments = new ObservableCollection<Assignment>();
Assignments.Add(new Assignment("todo 1", false));
Assignments.Add(new Assignment("todo 2", false));
Assignments.Add(new Assignment("todo 3", false));
}
/// <summary>
///
/// </summary>
public ICommand AddAssignmentCommand => new Command(AddAssignment);
public string NewAssignmentInputValue { get; set; }
void AddAssignment()
{
Assignments.Add(new Assignment(NewAssignmentInputValue, false));
}
/// <summary>
/// Remove the called assignment
/// </summary>
public ICommand RemoveAssignmentCommand => new Command(RemoveAssignment);
void RemoveAssignment(object o)
{
Assignment assignmentBeingRemoved = o as Assignment;
Assignments.Remove(assignmentBeingRemoved);
}
}
}
The Assignment model
using System;
using System.Collections.Generic;
using System.Text;
namespace PlannerApp
{
public class Assignment
{
public static void Init() { }
public string AssignmentName { get; set; }
public bool Complete { get; set; }
public Assignment(string AssignmentName, bool Complete)
{
this.AssignmentName = AssignmentName;
this.Complete = Complete;
}
}
}
Error: Xamarin.Forms.Xaml.XamlParseException: 'Position 30:37. Can not find the object referenced by Assignments'
When I remove the Command and CommandParameters from the ImageButton it runs and renders just fine but as soon as I bind it the program runs but drops the above error.
1条答案
按热度按时间whhtz7ly1#
不可能是
x:Reference Assignments
,因为Assignments
不是UI元素。将UI元素绘制为从XAML顶部开始的树。x:Reference
必须引用树中较高位置的某个xaml元素。不能引用viewmodel属性。也许应该是
x:Reference AssignmentList
: