Change TagVisualization at runtime - Surface doesn't refresh
I am working on a WPF-Surface project and have already created two different TagVisualizations for one ByteTag. Now I want to switch externally (in a normal WPF-Window) between those visualizations (at runtime).
My temporary solution: For a start I have just written a method, that adds or changes the current visualization (points to different xaml-files) after clicking the external window.
private void addTagVisualization(String tagvisualization)
{
ByteTagVisualizationDefinition def1 = new ByteTagVisualizationDefinition
{
Value = 192,
Source = new Uri(tagvisualization, UriKind.Relative),
UsesTagOrientation = true,
TagRemovedBehavior = TagRemovedBehavior.Fade,
PhysicalCenterOffsetFromTag = new Vector(0, 0)
};
TagVisualizer tagvis1 = new TagVisualizer();
tagvis1.Definitions.Add(def1);
grid.Children.Add(tagvis1);
}
The problem:
When I Click "Change Tagvisualization" in my WPF Window, the Surface gets the new connection but doesn't refresh - so I have to put the Tag up and down to see the new visualization. How can I make the surface recognize an object new - even when i开发者_开发问答t's already on the surface and doesn't move?
I tried everything: invalidateVisual, children.clear(), load the Files via XAMLReader - it's always the same: I have to put the tag up and down to see the new visualization. I think it's the same problem, like in all the Examples: the tags work, when they are moved around after an example has started. But when I put the tag on the surface before choosing the example - the Application doesn't get the tag....
Does anyone know what to do? I know that this works in the Core-Layer somehow, but there has to be a solution in WPF, too??
Thanks in advance!
Second attempt:
I tried to save the contact, to capture it later. It worked a bit better: after I click changeTagVisualization, the tag doesn't lose its "recognition" but only its visualization. After the click it looks like the tag has no visualization but is recognized by the surface. When I put it up and down, the new TagVisualization appears. What's the missing link??
The XAML-code for the main Window:
<s:SurfaceWindow (Namespacedefinitions...)>
<Grid x:Name="grid" s:Contacts.GotContactCapture="gotIt" />
</s:SurfaceWindow>
The C#-Code for the main Window:
// Here some global parameters
TagVisualizer tagvis1;
Contact tagContact;
int style = 1;
...
// A method that saves the tag, when added the first time to surface
private void gotIt(object sender, ContactEventArgs e){
tagContact = e.Contact
}
...
// The method which switches between the XAML-Files and loads different visualizations
private void addTagVisualization() {
ByteTagVisualizationDefinition defByte = ByteTagVisualizationDefinition();
defByte.value = 9;
defByte.LostTagTimeout = 0;
if(style == 1) {
defByte.Source = new Uri("TagVisualization1.xaml", UriKind.Relative)
}
else {
defByte.Source = new Uri("TagVisualization2.xaml", UriKind.Relative)
}
this.tagvis1 = new TagVisualizer();
tagvis1.Definitions.Add(defByte);
grid.Children.Add(tagvis1);
}
// I think here is the mistake: a method, which is called from my external window.
// after a click on changeVisualization the style parameter switches
// I clear the old visualization in order to add it once again with a different xaml
// then I capture the object and invalidate
public void setStyle(int style) {
this.style = style;
grid.Children.Clear();
addTagVisualization();
tag.Capture(tagvis1);
base.InvalidateVisual();
}
Imagine you added a button to your window and placed that button underneath the mouse even though the mouse button was already pressed. The button wouldn't automatically 'click' if the mouse button was already pressed when the button appeared beneath it.
TagVisualizer is the same way - it only pays attention to tags that go down on it or that it's formally given control of. So how do you formally give an element formal control of a contact? That's where 'capture' comes in. Find the Contact object that represents the tag and then call contact.Capture(tagvisualizer)
. That should do the trick!
Keep in mind though that probably a better design than dynamically creating new TagVisualizers would be to simply modify the Source property of your visualizationdefinition. Another alternaitve would be to dynamically change the contents of the tagvisualization element itself.
精彩评论