- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
考虑这段代码:
<UserControl x:Class="MyApp.MyControl"
...
xmlns:local="clr-namespace:MyApp"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<UserControl.Template>
<ControlTemplate>
<ControlTemplate.Resources>
<Storyboard x:Key="MyStory">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
<SplineColorKeyFrame KeyTime="0:0:1" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
...
</Border>
<ControlTemplate.Triggers>
<Trigger SourceName="brdBase" Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MyStory}"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
</UserControl>
上面的代码没有问题。现在,我想绑定(bind) MyStory
的关键帧值到此用户控件的 DP(名为 SpecialColor
),如下所示:
<Storyboard x:Key="MyStory">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
<SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
这会出错:
无法卡住此 Storyboard 时间线树以供跨线程使用。
可以使用隐藏代码来做到这一点。但我如何才能仅在 XAML 中执行此操作?
代码隐藏辅助解决方案:
►第 1 步:将 MyStory
Storyboard进入brdBase
资源。
<UserControl.Template>
<ControlTemplate>
<Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
<Border.Resources>
<Storyboard x:Key="MyStory">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
<SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Border.Resources>
...
</Border>
<ControlTemplate.Triggers>
<Trigger SourceName="brdBase" Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MyStory}"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
错误: 找不到名为“MyStory”的资源。资源名称区分大小写。
►第 2 步:消除 Trigger
在 IsMouseOver
属性并开始 MyStory
来自代码背后。
<UserControl.Template>
<ControlTemplate>
<Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black" MouseEnter="brdBase_MouseEnter">
<Border.Resources>
<Storyboard x:Key="MyStory">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
<SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Border.Resources>
</Border>
</ControlTemplate>
</UserControl.Template>
C# 代码隐藏:
private void brdBase_MouseEnter(object sender, MouseEventArgs e)
{
Border grdRoot = (Border)this.Template.FindName("brdBase", this);
Storyboard story = grdRoot.Resources["MyStory"] as Storyboard;
story.Begin(this, this.Template);
}
►第 3 步: 解决方案已经完成,但第一次不起作用。幸运的是,这个问题有一个解决方法。放ControlTemplate
就够了在Style
.
(我需要其他 Trigger
类型而不是 EventTrigger
并且必须用 UserControl
包装 ControlTemplate
元素。)
更新:
关于使用 ObjectDataProvider
的想法失败了。
代码如下:
<UserControl.Template>
<ControlTemplate>
<ControlTemplate.Resources>
<local:StoryboardFinder x:Key="StoryboardFinder1" AssociatedControl="{Binding ElementName=brdBase}"/>
<ObjectDataProvider x:Key="dataProvider" ObjectInstance="{StaticResource StoryboardFinder1}" MethodName="Finder">
<ObjectDataProvider.MethodParameters>
<sys:String>MyStory</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</ControlTemplate.Resources>
<Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
<Border.Resources>
<Storyboard x:Key="MyStory">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
<SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Border.Resources>
...
</Border>
<ControlTemplate.Triggers>
<Trigger SourceName="brdBase" Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource dataProvider}"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
StoryboardFinder 类:
public class StoryboardFinder : DependencyObject
{
#region ________________________________________ AssociatedControl
public Control AssociatedControl
{
get { return (Control)GetValue(AssociatedControlProperty); }
set { SetValue(AssociatedControlProperty, value); }
}
public static readonly DependencyProperty AssociatedControlProperty =
DependencyProperty.Register("AssociatedControl",
typeof(Control),
typeof(StoryboardFinder),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None));
#endregion
public Storyboard Finder(string resourceName)
{
//
// Associated control is always null :(
//
return new Storyboard();
}
}
最佳答案
如果这个代码是真的呢?
<UserControl x:Class="MyApp.MyControl"
...
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:l="clr-namespace:MyApp"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<UserControl.Resources>
<Style TargetType="{x:Type l:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:MyControl}">
<Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
<Border.Resources>
<Storyboard x:Key="MyStory">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
<SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type l:MyControl}}, Path=SpecialColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Border.Resources>
<i:Interaction.Triggers>
<l:InteractiveTrigger Property="IsMouseOver" Value="True">
<l:InteractiveTrigger.CommonActions>
<BeginStoryboard Storyboard="{StaticResource MyStory}"/>
</l:InteractiveTrigger.CommonActions>
</l:InteractiveTrigger>
</i:Interaction.Triggers>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
</UserControl>
如果是这样,我可以在 IsMouseOver
上设置触发器属性(property)...
我很高兴地说这是一个工作代码 :) 我只能使用 EventTrigger
在<Border.Triggers>
标签。这是限制。所以我开始考虑这个想法:如果我可以有一个可以在 FrameworkElement.Triggers
中工作的自定义触发器会怎样?范围?这是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
namespace TriggerTest
{
/// <summary>
/// InteractiveTrigger is a trigger that can be used as the System.Windows.Trigger but in the System.Windows.Interactivity.
/// <para>
/// Note: There is neither `EnterActions` nor `ExitActions` in this class. The `CommonActions` can be used instead of `EnterActions`.
/// Also, the `Actions` property which is of type System.Windows.Interactivity.TriggerAction can be used.
/// </para>
/// <para> </para>
/// <para>
/// There is only one kind of triggers (i.e. EventTrigger) in the System.Windows.Interactivity. So you can use the following triggers in this namespace:
/// <para>1- InteractiveTrigger : Trigger</para>
/// <para>2- InteractiveMultiTrigger : MultiTrigger</para>
/// <para>3- InteractiveDataTrigger : DataTrigger</para>
/// <para>4- InteractiveMultiDataTrigger : MultiDataTrigger</para>
/// </para>
/// </summary>
public class InteractiveTrigger : TriggerBase<FrameworkElement>
{
#region ___________________________________________________________________________________ Properties
#region ________________________________________ Value
/// <summary>
/// [Wrapper property for ValueProperty]
/// <para>
/// Gets or sets the value to be compared with the property value of the element. The comparison is a reference equality check.
/// </para>
/// </summary>
public object Value
{
get { return (object)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(object),
typeof(InteractiveTrigger),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, OnValuePropertyChanged));
private static void OnValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
InteractiveTrigger instance = sender as InteractiveTrigger;
if (instance != null)
{
if (instance.CanFire)
instance.Fire();
}
}
#endregion
/// <summary>
/// Gets or sets the name of the object with the property that causes the associated setters to be applied.
/// </summary>
public string SourceName
{
get;
set;
}
/// <summary>
/// Gets or sets the property that returns the value that is compared with this trigger.Value property. The comparison is a reference equality check.
/// </summary>
public DependencyProperty Property
{
get;
set;
}
/// <summary>
/// Gets or sets a collection of System.Windows.Setter objects, which describe the property values to apply when the trigger object becomes active.
/// </summary>
public List<Setter> Setters
{
get;
set;
}
/// <summary>
/// Gets or sets the collection of System.Windows.TriggerAction objects to apply when this trigger object becomes active.
/// </summary>
public List<System.Windows.TriggerAction> CommonActions
{
get;
set;
}
/// <summary>
/// Gets a value indicating whether this trigger can be active to apply setters and actions.
/// </summary>
private bool CanFire
{
get
{
if (this.AssociatedObject == null)
{
return false;
}
else
{
object associatedValue;
if (string.IsNullOrEmpty(SourceName))
associatedValue = this.AssociatedObject.GetValue(Property);
else
associatedValue = (this.AssociatedObject.FindName(SourceName) as DependencyObject).GetValue(Property);
TypeConverter typeConverter = TypeDescriptor.GetConverter(Property.PropertyType);
object realValue = typeConverter.ConvertFromString(Value.ToString());
return associatedValue.Equals(realValue);
}
}
}
#endregion
#region ___________________________________________________________________________________ Methods
/// <summary>
/// Fires (activates) current trigger by setting setter values and invoking all actions.
/// </summary>
private void Fire()
{
//
// Setting setters values to their associated properties..
//
foreach (Setter setter in Setters)
{
if (string.IsNullOrEmpty(setter.TargetName))
this.AssociatedObject.SetValue(setter.Property, setter.Value);
else
(this.AssociatedObject.FindName(setter.TargetName) as DependencyObject).SetValue(setter.Property, setter.Value);
}
//
// Firing actions..
//
foreach (System.Windows.TriggerAction action in CommonActions)
{
Type actionType = action.GetType();
if (actionType == typeof(BeginStoryboard))
{
(action as BeginStoryboard).Storyboard.Begin();
}
else
throw new NotImplementedException();
}
this.InvokeActions(null);
}
#endregion
#region ___________________________________________________________________________________ Events
public InteractiveTrigger()
{
Setters = new List<Setter>();
CommonActions = new List<System.Windows.TriggerAction>();
}
protected override void OnAttached()
{
base.OnAttached();
if (Property != null)
{
object propertyAssociatedObject;
if (string.IsNullOrEmpty(SourceName))
propertyAssociatedObject = this.AssociatedObject;
else
propertyAssociatedObject = this.AssociatedObject.FindName(SourceName);
//
// Adding a property changed listener to the property associated-object..
//
DependencyPropertyDescriptor dpDescriptor = DependencyPropertyDescriptor.FromProperty(Property, propertyAssociatedObject.GetType());
dpDescriptor.AddValueChanged(propertyAssociatedObject, PropertyListener_ValueChanged);
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (Property != null)
{
object propertyAssociatedObject;
if (string.IsNullOrEmpty(SourceName))
propertyAssociatedObject = this.AssociatedObject;
else
propertyAssociatedObject = this.AssociatedObject.FindName(SourceName);
//
// Removing previously added property changed listener from the associated-object..
//
DependencyPropertyDescriptor dpDescriptor = DependencyPropertyDescriptor.FromProperty(Property, propertyAssociatedObject.GetType());
dpDescriptor.RemoveValueChanged(propertyAssociatedObject, PropertyListener_ValueChanged);
}
}
private void PropertyListener_ValueChanged(object sender, EventArgs e)
{
if (CanFire)
Fire();
}
#endregion
}
}
我还创建了其他触发器类型(即 InteractiveMultiTrigger
、 InteractiveDataTrigger
、 InteractiveMultiDataTrigger
)以及一些其他操作,这使得有条件和多条件 EventTriggers 成为可能。如果你们专业人士确认这个解决方案,我会把它们全部发布。
感谢您的关注!
关于c# - 如何从 XAML 访问元素资源中的 Storyboard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15848381/
我在 iOS 项目中创建了一个新目标并添加了一些我的文件以及一个新的 Storyboard文件。但是当在模拟器中运行时(我还没有尝试过该设备)它会吐在 UIApplicationMain(argc,
The document "Storyboard.storyboard" could not be opened. Could not read archive. Please use a newer
我的 .travis.yml 文件: language: objective-c script: xctool -project ProjectName. xcodeproj -scheme
我正在使用 Storyboard,因为我有一个定制的表格。我需要在单元格内的标签上显示值。我尝试为它创建一个 IBOutlet,但它似乎不接受它,它给了我一个错误消息,“连接“xyz”不能将原型(pr
Xcode 6 界面生成器默认有新的复选框“使用大小类别”。它使 View 具有适应性。 当我尝试在 Storyboard中的两个 View 之间进行连接时,我有新的选择: 代替旧的: 现在我们有“s
我在新的 xcode beta 版本中打开我的项目时犯了一个错误 现在我无法用稳定版本打开它,因为 The document “Main.storyboard” requires at least X
如果我有一个已经使用 Storyboard 的 Xcode 项目,现在我想切换到以编程方式加载 View Controller ,我该怎么做? 最佳答案 这是一个in-depth article关于这
[错误]:**构建失败** [错误]:以下构建命令失败: [错误]:编译Storyboard LaunchScreen.storyboard [错误]:(1 次失败) 如何解决这个问题?它不会在 io
我试图找到一种方法来处理多个目标(从一个项目部署多个应用程序),我不知道如何定义UIColor并在 Storyboard 中使用它(我可以为UIImage做得很好)。我的想法是使用宏根据目标切换此颜色
我有一个在 Xcode 5.1 中构建的带有 2 个 Storyboard的项目。 当我在 Xcode 6 中打开其中任何一个时,我收到此错误。他们从未在任何测试版中打开过,所以我认为这不是 Xcod
我想将 StoryBoard 添加到以编程方式制作的项目中,是否可以这样做。 最佳答案 是的,您可以向 Xcode 项目添加新的 Storyboard。我正在分享以下屏幕截图,请检查: 您可以为该 S
我将几个配置文件分成多个 Storyboard以尝试帮助我减少卡住时间,因为我听说许多 Storyboard可能是 Xcode 有时会“卡住”长达 10 分钟的原因,有时会导致我的流程崩溃工作的。所以
我是 iOS 开发新手,我知道这是一个非常简单的问题,但我真的无法理解原因。 当我构建 SingleViewApplication 时,Xcode 自动生成 LaunchScreen.storyboa
为什么 Storyboard.SetTargetName 有效而 Storyboard.SetTarget 无效?这里 xaml -
我使用的是 Xcode 7.1,我正在使用 Storyboard 引用转到不同的 Storyboard 。问题是每次我运行模拟器时,都会出现一个错误,提示 “没有找到从 Main.storyboard
因此,我开始在XCode 4.3.2中使用 Storyboard 。我从Master-Detail应用程序(iPad的拆分 View 应用程序)开始。在详细信息 View (名为DetailVC)中,
我想在我的应用程序中加载辅助 StoryboardTest.storyboard(在Main.storyboard旁边),如下所示: let storyboard = UIStoryboard(nam
我有情节提要,其中假设Controller1有按钮 当我们按下按钮时,它应该移至下一个控制器,但是我无法访问它。能告诉我如何访问第二个Storybaord,其中一个名为Profile的TabbarCo
这是我在尝试启动主 Storyboard时看到的内容,并且 launchscreen.storyboard 也遇到同样的问题。 当我尝试访问 main.storyboard 时,我看到的是 XML 代
我在 main.storyboard 的 View Controller 中有一个 label1我在 Storyboard的 View Controller 中有另一个名为 Second.storyb
我是一名优秀的程序员,十分优秀!