gpt4 book ai didi

wpf - Caliburn Micro,绑定(bind)和通知

转载 作者:行者123 更新时间:2023-12-03 16:29:41 28 4
gpt4 key购买 nike

经过几个月的 WPF 实践后,我决定试一试 Caliburn Micro。我有几件事情我不能上类。我将首先发布代码,请参阅下面的问题/问题:

public class MainViewModel : PropertyChangedBase
{
BindableCollection<PlayerProfile> _playerProfiles = staticInfos.Load();

public BindableCollection<PlayerProfile> PlayerProfiles {
get { return _playerProfiles; }
set {
_playerProfiles = value;
NotifyOfPropertyChange(() => PlayerList);
}
}

string _tb_AddPlayer;
public string TB_AddPlayer {
get { return _tb_AddPlayer; }
set {
_tb_AddPlayer = value;
NotifyOfPropertyChange(() => TB_AddPlayer);
NotifyOfPropertyChange(() => CanAddPlayer);
}
}

List<string> _pl = new List<string>();
public List<string> PlayerList {
get{
foreach (PlayerProfile p in PlayerProfiles) {
if (!_pl.Contains(p.Name)) _pl.Add(p.Name);
}
return _pl;
}
set {
_pl = value;
NotifyOfPropertyChange(()=>PlayerList);
}
}

// Dummy Test String
string _test;
public string Test {
get { return _test; }
set {
_test = value;
NotifyOfPropertyChange(() => Test);
}
}

// Button Action
public void AddPlayer() {
_playerProfiles.Add(new PlayerProfile(TB_AddPlayer));
Test = "Pressed";
NotifyOfPropertyChange(() => PlayerProfiles);
}

public bool CanAddPlayer {
get { return true; }
// doesnt work: get { return !string.IsNullOrWhiteSpace(TB_AddPlayer); }
}
}

所以这里是:我使用绑定(bind)约定,即 MainView 中的属性应该绑定(bind)到 View 中同名的元素。
  • 首先,请注意 PlayerList只是 PlayerProfiles 中的玩家名称列表,我创建它是因为当绑定(bind)到 Combobox ,当我命名 Combobox PlayerProfiles 时它们不会出现,但是当我浏览列表并将其命名为 PlayerList 时,他们会这样做- 虽然我已经覆盖了 ToString PlayerProfile class 中的方法.为什么?这看起来很笨拙...(如果您想知道,staticInfos.Load() 方法会使用几个虚拟名称填充 PlayerProfiles...)
  • 当我在文本框中输入内容(称为 TB_AddPlayer )并按下按钮(称为 AddPlayer )时,会出现测试字符串,因此我知道发生了操作,但新名称未出现在组合框中
  • 另外,如果我使用 CanAddPlayer 中的注释行属性,按钮永远不会被启用,如果我开始输入,也不会当文本框失去焦点时,其中的文本。为什么?

  • 对于这些基本问题,我很抱歉,我已经盯着“Hello World”caliburn 示例看了好几个小时,看不到我遗漏了什么……谢谢。提前!

    编辑:这是 .xaml

    <UserControl x:Class="MixGameM8_MVVM.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MixGameM8_MVVM.Views">

    <UserControl.Resources>
    <Style TargetType="TextBlock" x:Key="TB_A">
    <Setter Property="Margin" Value="5,5,5,5" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="FontSize" Value="15" />
    </Style>

    <!-- ... Some more of this kind -->
    </UserControl.Resources>

    <Grid>
    <!-- ... Grid definitions -->
    <Border Style="{StaticResource Border_A}" Grid.Row="0" Grid.Column="0">
    <StackPanel Name="SP_Controls">
    <TextBlock Style="{StaticResource TB_A}" Text="Controls"/>
    <ComboBox Style="{StaticResource CB_A}" Name="PlayerProfiles"/>
    <StackPanel Orientation="Horizontal">
    <TextBox Style="{StaticResource TBox_A}" Name="TB_AddPlayer" MinWidth ="100" />
    <Button Style="{StaticResource Button_AddPlayer}" Content="Add Player" Name="AddPlayer" />
    </StackPanel>
    </StackPanel>
    </Border>

    <!-- ... And some more cells in the grid, one containing the dummy textbox -->

    </Grid>
    </UserControl>

    最佳答案

    首先,我将首先删除您的 PlayerList属性,并更新您的 PlayerProfiles像这样的属性(property):

     public BindableCollection<PlayerProfile> PlayerProfiles {
    get { return _playerProfiles; }
    set {
    _playerProfiles = value;
    NotifyOfPropertyChange(() => PlayerProfiles);
    }
    }

    接下来,我不会将您的 View 模型属性称为描述它们如何呈现的东西,即更改 TB_AddPlayer只需 AddPlayer (或者更好的是像 PlayerName 这样的东西)。

    接下来,如果您调用您的 ComboBox PlayerProfiles ,那么绑定(bind)应该通过约定自动发生。在您的 AddPlayer 方法中,您希望通过您的属性添加新的玩家资料,而不是支持字段:

    改变:
    public void AddPlayer() {
    _playerProfiles.Add(new PlayerProfile(TB_AddPlayer));
    Test = "Pressed";
    NotifyOfPropertyChange(() => PlayerProfiles);
    }

    到:
    public void AddPlayer() {
    this.PlayerProfiles.Add(new PlayerProfile(this.PlayerName));
    }

    因为 PlayerProfiles 是一个 BindableCollection,所以将执行更改通知,并且您的 ComboBox 将被更新。

    此外,我总是会为您的支持字段明确指定访问修饰符,即 private string _playerName;不仅仅是字符串 _playerName;
    尝试这些更改,如果仍有问题,请更新问题中的代码。

    关于wpf - Caliburn Micro,绑定(bind)和通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13011406/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com