问候,
我想直观地显示后台进程何时工作。此过程定期发生(例如每 30 秒),可能需要 10 毫秒或 1000 毫秒以上才能完成。我正在使用 MVVM-Light 框架,因此创建了一个数据触发器,连接到淡入和淡出的心脏图像上的视图模型属性。
当该过程需要一秒或更长的时间时,我的业余动画技术正在发挥作用,但我也希望它在该过程需要很短的时间(<100ms)时完成完整的心跳(2次重复),否则动画也会结束很快,您无法(从视觉上)看出该过程正在运行。
问题是心脏应该在整个过程中保持跳动,所以我不能只将重复行为设置为 2。XAML 解决方案是首选,但如果需要一些后面的代码,我不会畏缩:)
<Image
Height="60" Width="60" Margin="0,6,6,6"
Name="Heartbeat" Source="/Resources/Heartbeat.png"
VerticalAlignment="Bottom" HorizontalAlignment="Right"
Opacity=".05" Stretch="UniformToFill">
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsHeartBeating}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="HeartbeatStoryboard">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0.05" To="0.8" Duration="0:0:0.100">
</DoubleAnimation>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0.8" To="0.05" Duration="0:0:0.300">
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseOut" Power="6" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="HeartbeatStoryboard" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Run Code Online (Sandbox Code Playgroud)
如果有人对如何改进心跳动画有任何建议,我愿意接受建议,谢谢!
EventToCommand 无法在加载事件上传递命令参数
当附加到页面或用户控件的 Load 事件时,EventToCommand 成功调用 ViewModel 中的处理程序,但不传递 CommandParameter。但是,相同的 XAML 附加到另一个事件,例如按钮单击,命令处理程序接收数据绑定数据作为其参数。Xml:
<i:EventTrigger EventName="Loaded" SourceObject="{Binding ElementName=Control}">
<Command:EventToCommand x:Name="etcLoad"
Command="{Binding LoadCommand}"
CommandParameter="{Binding Target, ElementName=Control}" />
</i:EventTrigger>
目标是视图上的字符串 DP。
虚拟机代码:
internal void Load(string p_Param)
{
this.Initialise();
}
public RelayCommand<string> LoadCommand { get; private set; }
Run Code Online (Sandbox Code Playgroud)
并且命令被分配如下:
this.LoadCommand = new RelayCommand<string>(this.Load);
Run Code Online (Sandbox Code Playgroud)
我几乎可以肯定,问题在于绑定晚于分配给目标 DP 或类似的东西。我有兴趣为此尽快找到解决方案,或者以其他方式从 View 中获取字符串并进入 ViewModel,其中从 OnNavigateTo 覆盖分配字符串。目标是根据通过 URI 提供的查询属性提供选项卡的选择,即“/Views/DisplayTabDetails?Tab=Tab1”或类似的。
我想知道使用relay命令调用刷新屏幕的函数有什么好处。在我的应用程序中,我有以下中继命令设置。
private RelayCommand _refreshSitesCommand;
public RelayCommand RefreshSitesCommand
{
get { return _refreshSitesCommand ?? (_refreshSitesCommand = new RelayCommand(RefreshSites)); }
}
private RelayCommand _refreshProvidersCommand;
public RelayCommand RefreshProvidersCommand
{
get { return _refreshProvidersCommand ?? (_refreshProvidersCommand = new RelayCommand(RefreshProviders)); }
}
private async void RefreshSites()
{
var sitesStats = await _dataService.GetSiteStats();
if (sitesStats != null)
{
SiteStats.Clear();
foreach (var site in sitesStats)
{
SiteStats.Add(new SiteStatsViewModel(site));
}
SelectedSite = SiteStats[0];
}
}
private async void RefreshProviders()
{
var providers = await _dataService.GetProviders();
if (providers != …Run Code Online (Sandbox Code Playgroud) 我想学习最正确的方法来解决这个问题:我的 GameView 中有一个绑定到ObservableCollection<Adventurer>. 双击单元格后,我需要一个新窗口(或其他更合适的窗口)来打开并显示有关Adventurer根据单元格正确的数据。到目前为止我还做不到。这就是我到目前为止所拥有的(并不多,但我尝试过的一切都没有奏效)。
我的 ListView 中的触发器/命令 GameView.xaml
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=ShowAdvCommand}"
CommandParameter="{Binding ElementName=AdvListView,
Path=SelectedItem}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
以及 GameViewModel.cs 中的命令
ShowAdvCommand = new RelayCommand<Adventurer>(p =>
{
System.Windows.MessageBox.Show(p.Name);
});
Run Code Online (Sandbox Code Playgroud)
MessageBox 只是为了确认 Eventtocommand 正在工作。
我本质上需要一个容器,它可以Adventurer在双击 Listview 单元格后将正确的参数作为参数,并允许我显示特定于该实例的数据。我也更愿意坚持对 MVVM 友好的东西。
任何建议将不胜感激。
更新:我可能取得了一些进展:
游戏视图模型:
ShowAdvCommand = new RelayCommand<Adventurer>(p =>
{
AdventurerView adv = new AdventurerView(p);
adv.Show();
});
Run Code Online (Sandbox Code Playgroud)
冒险家视图:
public partial class AdventurerView : Window
{
Adventurer adv;
public AdventurerView(Adventurer adv)
{
this.adv …Run Code Online (Sandbox Code Playgroud) 我有一个包含以下代码的列表视图:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="allToDoItemsListBox"
ItemsSource="{Binding AllToDoItems,Mode=OneWay}"
Margin="12,0,12,0"
Width="440"
ItemTemplate="{StaticResource ToDoListBoxItemTemplate}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
数据模板如下:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="ToDoListBoxItemTemplate">
<Grid HorizontalAlignment="Stretch" Width="420">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<CheckBox
IsChecked="{Binding IsComplete, Mode=TwoWay}"
Grid.Column="0" VerticalAlignment="Top"/>
<TextBlock
Text="{Binding ItemName}"
FontSize="{StaticResource PhoneFontSizeLarge}"
Foreground="Gray"
Grid.Column="1" Grid.ColumnSpan="2"
VerticalAlignment="Top" Margin="-36, 12, 0, 0"/>
<Button
Grid.Column="3"
x:Name="deleteTaskButton"
BorderThickness="0"
Margin="0, -18, 0, 0"
Command="{Binding Path=DeleteCommand}" CommandParameter="{Binding}"/>
<Image
Source="/Images/appbar.delete.rest.png"
Height="75"
Width="75"/>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
Run Code Online (Sandbox Code Playgroud)
我尝试将按钮命令“DeleteCommand”绑定到 ViewModel 中的 ICommand,但失败了,我该怎么办?
ViewModel 中的代码是:
public …Run Code Online (Sandbox Code Playgroud) 在 MVVM Light WPF 应用程序中实现运行时本地化更改功能的正确方法是什么?我正在使用基于资源的本地化(.resx 文件),我希望几乎可以随时更改语言环境。
我虽然在第一次约在做全局属性App.xaml.cs与LocalizationChanged每个查看有线了事件,但这种违反MVVM至少两个规则:没有在后台代码和耦合View类在一起,因为他们会在App类中依赖(这将更难对它们进行单元测试)
另一个想法是创建ILocalizableModel将由LocalizableModel类实现的全局接口(在运行时通过 mvvm light magic 注入),然后提供接口来注册LocalizationChanged事件和设置新本地化的方式(可能还有一些其他功能,例如枚举可用的本地化)。该事件将在 ViewModel 类中触发并请求它们更新所有属性。这还有另一个问题:某些视图数据(如列表)必须重新创建才能更新。在 ViewModel 中处理强特定于视图的代码似乎也有点扭曲。
另一个想法是在模型中使用观察者模式或自动属性。
但是什么是“正确”的 MVVM 方法呢?
我有一组正确显示的单选按钮(如下图所示)。现在,当我点击它们时,它们周围似乎出现了一个红色方块(如下图 2 所示)。这让我想起了 wpf 的渲染问题,因为我之前看到的与 winforms 类似。
至于开发,我使用 wpf 和 c#,mvvmlight。
另外 iv 在下面包含了我的代码:
<RadioButton GroupName="Part_GrpPlayBtn"
ToolTip="Play"
Style="{StaticResource StyleRadioButton}"
Template="{StaticResource Template_Play}"
IsChecked="{Binding PlayState, Converter={StaticResource PlayStateToCheckedConvert}, ConverterParameter={x:Static local:ePlaybackState.ePlay}}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked" >
<i:InvokeCommandAction Command="{Binding PlayCommand,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:VMSPlayBarControl}}}" >
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<Style TargetType="RadioButton" x:Key="StyleRadioButton" >
<Setter Property="Margin" Value="5" />
<Setter Property="ToolTipService.IsEnabled" Value="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>
</Style>
<ControlTemplate x:Key="Template_Play" TargetType="{x:Type RadioButton}">
<Grid>
<Ellipse x:Name="Part_BgEllipse" Style="{StaticResource StyleEllipse}" />
<Image x:Name="PART_Image" Source="{StaticResource ImgPlayOff}" Style="{StaticResource StyleImage}"/>
<ContentPresenter/>
</Grid>
<ControlTemplate.Triggers> …Run Code Online (Sandbox Code Playgroud) 我有以下控制。我想更改某些事件触发器的背景颜色。我想clrGray在某些事件点击时将此资源设置为颜色。
我已经尝试过以下方法,但没有成功:(
XAM:
<local:RoundedFrame x:Name="MyFrame1" HeightRequest="16" IsVisible="True" BackgroundColor="{DynamicResource clrGreen}">
Run Code Online (Sandbox Code Playgroud)
CS:
//On Some event
//Not working
MyFrame1.SetDynamicResource(MyFrame1.BackgroundColor, "clrGreen");
Run Code Online (Sandbox Code Playgroud) 我对一个(我认为)简单的实现感到困惑;根据与视图模型的绑定使 UI 元素可见。我使用 mvvmlight 框架。当绑定(布尔值)设置为true可见性时,绑定不会对更改做出反应。
XAML:
<Button
Command="{Binding NavigationCommand}" CommandParameter="{StaticResource Back}"
Visibility="{x:Bind (Visibility) ViewModel.ShowNavigationButtons}">
<Image Source="../../../Resources/NavigateBack.PNG"/>
</Button>
Run Code Online (Sandbox Code Playgroud)
后面的代码:
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
DataContext = new MainViewModel();
}
public MainViewModel ViewModel => DataContext as MainViewModel;
}
Run Code Online (Sandbox Code Playgroud)
视图模型:
public class MainViewModel : ViewModelBase
{
private bool _showNavigationButtons;
public RelayCommand BrakingCommand { get; }
public bool ShowNavigationButtons
{
get => _showNavigationButtons;
set { Set(() => ShowNavigationButtons, ref _showNavigationButtons, value); }
}
public MainViewModel()
{
BrakingCommand …Run Code Online (Sandbox Code Playgroud) 我有两个文本框,在我的 ViewModel 中我希望能够跟踪当前处于焦点的框。
<TextBox x:Name="textBox1" Text="Text Box 1"/>
<TextBox x:Name="textBox2" Text="Text Box 2"/>
Run Code Online (Sandbox Code Playgroud)
如何从我的 ViewModel 中读取/识别当前处于焦点的文本框?
mvvm-light ×10
c# ×7
wpf ×5
mvvm ×4
xaml ×2
binding ×1
data-binding ×1
load ×1
localization ×1
radio-button ×1
textbox ×1
uwp ×1
viewmodel ×1
visibility ×1