- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好的。我正在按照教程掌握我的应用程序的 MVVM 模式。
我已经很好地遵循了教程,我学习了命令部分,当我尝试将命令应用于按钮时它似乎根本不起作用,我的按钮从未禁用。我看过其他 MVVM 源代码,我的代码似乎还不错,除了我在互联网上发现本教程适用于 WPF,但它也适用于 UWP Windows 10 应用程序。
所以我将粘贴我的类和 Xaml View 。
这是我的模型:
using System.ComponentModel;
namespace MVVMDemo.Model
{
public class Student : INotifyPropertyChanged
{
private string firstName;
private string lastName;
public string FirstName
{
get
{
return firstName;
}
set
{
if (firstName != value)
{
firstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
}
public string LastName
{
get { return lastName; }
set
{
if (lastName != value)
{
lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
}
public string FullName
{
get
{
return firstName + " " + lastName;
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}
我相信模型代码没有错误。这是我的 View 模型:
using MVVMDemo.Model;
using System.Collections.ObjectModel;
namespace MVVMDemo.ViewModel
{
public class StudentViewModel
{
/*Let’s add a property of MyICommand type in StudentView Model class.
* Now we need to construct an instance in the StudentViewModel. We will
* use the overloaded constructor of MyICommand that takes two parameters.*/
public MyICommand DeleteCommand { get; set; }
/*Now add the implementation of OnDelete and CanDelete methods.*/
public StudentViewModel()
{
LoadStudents();
DeleteCommand = new MyICommand(OnDelete, CanDelete);
}
private bool CanDelete()
{
//if (SelectedStudent != null)
// return true;
//return false;
return SelectedStudent != null;
}
private void OnDelete()
{
Students.Remove(SelectedStudent);
}
/*We also need to add a new SelectedStudent so that the user
* can delete the Selected Item from ListBox.*/
private Student _selectedStudent;
public Student SelectedStudent
{
get
{
return _selectedStudent;
}
set
{
_selectedStudent = value;
DeleteCommand.RaiseCanExecuteChanged();
}
}
public ObservableCollection<Student> Students
{
get;
set;
}
public void LoadStudents()
{
ObservableCollection<Student> students = new ObservableCollection<Student>();
students.Add(new Student { FirstName = "Mark", LastName = "Allain" });
students.Add(new Student { FirstName = "Allen", LastName = "Brown" });
students.Add(new Student { FirstName = "Linda", LastName = "Hamerski" });
Students = students;
}
}
}
这是我的 ViewModelLocator:
using MVVMDemo.ViewModel;
namespace MVVMDemo.VML
{
public class ViewModelLocator
{
private static StudentViewModel studentViewModel = new StudentViewModel();
public static StudentViewModel StudentViewModel
{
get { return studentViewModel; }
}
}
}
最后这是我的 View 我的 UserControl StudentView
<UserControl
x:Class="MVVMDemo.Views.StudentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MVVMDemo.Views"
xmlns:vml="using:MVVMDemo.VML"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource mainViewModelLocator}, Path=StudentViewModel}"
d:DesignHeight="300"
d:DesignWidth="400">
<!--We will apply MVVM Using ItemTemplates-->
<UserControl.Resources>
<DataTemplate x:Key = "studentsTemplate">
<StackPanel Orientation = "Horizontal">
<TextBox Text = "{Binding Path = FirstName, Mode = TwoWay}"
Width = "100" Margin = "3 5 3 5"/>
<TextBox Text = "{Binding Path = LastName, Mode = TwoWay}"
Width = "100" Margin = "0 5 3 5"/>
<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}"
Margin = "0 5 3 5"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<!--In StudentView.xaml, we need to add SelectedItem property in a ListBox
which will bind to the SelectStudent property.-->
<ListView ItemsSource="{Binding Students}"
ItemTemplate="{StaticResource studentsTemplate}"
SelectedItem="{Binding SelectedStudent}"/>
<Button Content = "Delete"
Command="{Binding DeleteCommand}"
HorizontalAlignment = "Left"
VerticalAlignment = "Top"
Width = "75"/>
</StackPanel>
</Grid>
</UserControl>
我在应用程序级别的 app.xaml 文件上应用 viewmodellocator:
<Application.Resources>
<vml:ViewModelLocator x:Key="mainViewModelLocator"/>
</Application.Resources>
然后我这样调用主页上的 View :
有人可以帮助我吗,我已经在 VS2015 和 VS2017 上运行了这段代码,删除按钮从未被启用,它也没有从 ListView 或列表框中删除行我已经尝试更改它们但似乎没有什么不同。
我什至在不使用此处使用的中继命令模式的情况下进行了命令式实现。非常感谢您的帮助。
谢谢!!!
最佳答案
编辑:(这不是答案,而是另一种方法)
您知道您可以使用 {x:Bind} 将方法直接绑定(bind)到事件吗?您不需要使用 ICommand。
XAML
<Button Grid.Row="4"
x:Uid="Send"
VerticalAlignment="Top"
HorizontalAlignment="Left"
MinWidth="80"
Click="{x:Bind ViewModel.SendFeedback}"/>
代码隐藏
public async void SendFeedback()
{
// Code
}
用这种方式更容易:)
关于c# - UWP 的 MVVM 模式教程,但它不会绑定(bind)命令,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43154481/
uwp 和非 uwp 应用程序之间是否可以进行通信。我的非uwp是一个提供服务的后台任务。我想在 uwp 应用程序中调用该非 uwp 服务。 如何调用电话? AppServiceConnection
我正在开发一个应用程序,该应用程序旨在在具有多个显示器(从 1 到 3必要)在每个监视器上,每个监视器都显示不同的 View 。 就我所见,UWP 并不自然适用于这种情况。我设法使用 CoreAppl
我正在尝试对UWP应用使用broadFileSystemAccess功能,但是package.appxmanifest的功能列表中未列出broadFileSystemAccess功能。 我的最低和最高
有时我有一个打开的流或一个事件的队列,必须在应用程序关闭时正确处理。在 WPF 应用程序中,我可以在应用程序上使用 Unloading 事件,但在 UWP 中我注意到这样的事件不存在。 我如何可靠地做
http://wikisend.com/download/880354/UWP_Server.zip 我已将代码上传至上述网址。 它是 UWP 中的客户端和服务器应用程序。这里客户端和服务器都在同一个
大家好 我知道这个问题(Chromium working on UWP)之前(2015/2016)有人问过,想看看有没有更新 我正在尝试在 UWP 应用程序中使用 CEF3 构建,但在运行该应用程序时
我目前正在构建一个应用程序,它可以使用 Windows 游戏 DVR 在某个时刻开始录制屏幕。该录音机在完成录音时将应用程序名称作为文件名。 我发现了如何使用 Applicationview.GetF
我已使用 Desktop App Converter 将我的 WPF 应用程序转换为 appx 包。我需要在资源管理器上下文菜单中有一个项目。 IE。用户右键单击文件并在主菜单中看到我的项目“对应用程
我想稍微修改一个 Button 控件(添加描述)。在哪里可以找到 UWP 控件的默认控件模板? 最佳答案 似乎可以在以下位置找到它们: C:\Program Files (x86)\Windows K
我想通过 UWP 应用访问 windows10 注册表项。 键为:\HKEY_LOCAL_MACHINE\SOFTWARE\MyCompanyName\MyName 我找不到任何函数调用来完成它。 请
我开发了一个 UWP appx,它可以安装在 cmd.exe 提示符中: C:\test>myapp.appx 但是在安装过程中会弹出一个 Windows GUI。 有没有什么方法 使用 Silent
在我的 UWP 应用程序中,如何通过 UpdateTask 中的代码进行调试? VS 2017 中的“生命周期事件”下拉菜单似乎没有提供触发此类后台任务的选项。有办法实现吗? 最佳答案 首先是关于 U
我尝试在 VS 2017 中创建一个 UWP 应用程序包。 创建时我收到一条神秘的错误消息:严重性代码描述项目文件行抑制状态错误 0xdef00532 - 资源“Files/Assets/Square
我有一个 TextBlock在我的应用程序中。我要办理pinch in & out在它上面调整字体的大小。当ManipulationDelta事件触发我检查Scale属性(property),但大多数
为什么默认选择的索引不起作用?它因平台异常而崩溃: RumTime 错误: Exception thrown at 0x00007FFDEF7F7788 (KernelBase.dll) in ab
有没有办法在同一个包中的 UWP 应用程序和桌面桥应用程序之间共享互斥锁?它们似乎有不同的命名空间;使用相同的名称不会在进程之间产生相同的对象。根据 WinObj,UWP 应用程序的对象是,存储在 A
有什么方法可以检测当前的 UWP 要退出 ? (由用户关闭或终止进程) 我想向服务器发送一些关于应用程序将断开连接的请求,还想在退出之前保存一些数据。 最佳答案 无法检测这种情况或阻止用户终止您的应用
我正在使用 XAML 和 C# 开发通用 Windows 平台应用程序。我想在 UWP 中更改焦点上 TextBox 的边框颜色。 在此先感谢您的帮助。 最佳答案 其实实现起来很简单,按照以下步骤即可
是否可以在 UWP 应用中更改甚至隐藏鼠标指针? 我唯一能找到的是: Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = null; 但
我是一名优秀的程序员,十分优秀!