0

Question, how can we set a value for a property on Visual without using an animation? For example this works for me (for a Translation) but I feel there must be a better way. I'm using an animation of 1 millisecond to set a value immediately:

ElementCompositionPreview.SetIsTranslationEnabled(MyRectangle, true);

var visual = ElementCompositionPreview.GetElementVisual(MyRectangle);
var anim = compositor.CreateScalarKeyFrameAnimation();

anim.InsertKeyFrame(1.0f, 100f);
anim.Duration = TimeSpan.FromMilliseconds(1);

visual.StartAnimation("Translation.Y", anim);
Maximus
  • 1,441
  • 14
  • 38

2 Answers2

1

The document has already said translation is not on visual: These properties have the same purpose and behavior as the like-named properties on the composition Visual class (except for Translation, which isn't on Visual).


Edit:

First of all, thanks Maximus for sharing:

ElementCompositionPreview.SetIsTranslationEnabled(MyRectangle, true);

I'm still not quite sure about your specific problem right now. But for the question of the title: "How to set a property without using an animation", do you mean something like this:

ElementCompositionPreview.SetIsTranslationEnabled(MyRectangle, true);
  var myInteropVisualPropertySet = ElementCompositionPreview.GetElementVisual(MyRectangle).Properties;
  myInteropVisualPropertySet.InsertVector3("Translation", new Vector3(0f,100f,0f));

In addition, I have the following code example for the "Suppose and were to-to-be Offset as you stated, is and a-to-set it directly with animation" question:

private void MyRectangle_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var compositor = Window.Current.Compositor;
            Visual visual = ElementCompositionPreview.GetElementVisual(MyRectangle);
            var vertical = compositor.CreateVector3KeyFrameAnimation();
            vertical.InsertKeyFrame(1.0f, new Vector3(0f, 100f, 0f));
            vertical.Duration = TimeSpan.FromMilliseconds(1);
            visual.StartAnimation("Offset", vertical);
        }

By the way, if you are just focus on animate a UI element(not visual), community toolkit contains a easy function for offset

Barry Wang
  • 1,459
  • 1
  • 8
  • 12
  • The property does in fact exist because the Composition team added the following method: ElementCompositionPreview.SetIsTranslationEnabled(MyRectangle, true); This special method was a fix to "inject" the "Translation" property into the Visual's property set. So to answer, yes it works and yes it's production ready. But aside from, suppose we were to use Offset as you stated, is there a way to set it directly with animation? – Maximus May 01 '19 at 14:17
  • Here is the explanation related to the property injection: https://github.com/Microsoft/WindowsCompositionSamples/issues/145#issuecomment-286603195 – Maximus May 01 '19 at 14:19
  • @Maximus Thanks for sharing the info, I've updated my answer about some of my ideas about your question, please check whether they are what you need – Barry Wang May 02 '19 at 05:49
  • Ahh, I see what you did, you grabbed the PropertySet and set it directly, ok I'll give that a try, hope it work, thank you ! – Maximus May 03 '19 at 14:40
1

Here is an example of what I did based on @Barry Wang's answer:

var props = ElementCompositionPreview.GetElementVisual(MyRectangle).Properties;

if (props.TryGetVector3("Translation", out var vect) == CompositionGetValueStatus.Succeeded)
{
    props.InsertVector3("Translation", new Vector3(vect.X, 150f, vect.Z));
}
Maximus
  • 1,441
  • 14
  • 38