locked
How to get a "StaticResource ApplicationTextBrush" for line drawing with C++/CX ?

    Question

  • The immediate question is how do I set the Stroke property of a Polyline with an ApplicationTextBrush in C++/CX ?

    It arises because I want to implement this bit of XAML at runtime in C++ so that I can add multiple lines to a panel. My current understanding is that there is no direct DrawLine function, but that you create a PolyLine class instance and append it to the element instead.

           <StackPanel x:Name="myCanvasPanel" HorizontalAlignment="Right" VerticalAlignment="Center">
                <Polyline x:Name="myLine" Stroke="{StaticResource ApplicationTextBrush}" StrokeThickness="3" >
                    <Polyline.RenderTransform>
                        <CompositeTransform ScaleY="-1"/>
                    </Polyline.RenderTransform>
                </Polyline>
            </StackPanel> 

    I was able to convert most of the code except for the StaticResource  ApplicationTextBrush.  I did a SolidColorBrush instead while I'm stuck.

    void addLine( Windows::UI::Xaml::Controls::Panel^ element, Media::PointCollection^ points, Windows::UI::Color color )
    {
    	auto dynPolyLine = ref new Windows::UI::Xaml::Shapes::Polyline();
    
    	auto brush= ref new Windows::UI::Xaml::Media::SolidColorBrush(color);
    	dynPolyLine->Stroke= brush;
    	dynPolyLine->StrokeThickness= 3;
    
    	auto renderTransform = ref new Windows::UI::Xaml::Media::CompositeTransform();
    	renderTransform->ScaleY= -1;
    	dynPolyLine->RenderTransform= renderTransform;
    
    	dynPolyLine->Points= points;
    
    	element->Children->Append( dynPolyLine );
    }


    Friday, May 25, 2012 2:56 PM

Answers

  • Thank you; I used your code snippet, and found a one line approach- just look up the resource "ApplicationTextBrush" as a brush.

    	Media::Brush^ brush= dynamic_cast<Media::Brush^>( App::Current->Resources->Lookup( "ApplicationTextBrush" ) );
    

    I tested it with both light and dark Requested Themes, and I saw the desired dark and light lines.

    • Marked as answer by Andrew7Webb Friday, May 25, 2012 6:59 PM
    Friday, May 25, 2012 6:41 PM

All replies

  • Can you try the following and let me know if it works?

    Style^ style = dynamic_cast<Style^>(App::Current->Resources->Lookup("BasicRichTextStyle"));

    SetterBaseCollection^ coll = style->Setters;

    Setter^ b = (Setter^)coll->GetAt(0);

    myLine->Stroke = (Brush^)b->Value;

    Basically, if you look into the StandardStyles.xaml, you will notice RichTextBlock styles defined. What we are doing with the above code snippet, is to load the RichTextBlock style and then get the first setter and read its value. Obviously I hard coded the GetAt to Index 0, ideally one should be iterating over the collection and reading the values.

    Friday, May 25, 2012 6:31 PM
  • Thank you; I used your code snippet, and found a one line approach- just look up the resource "ApplicationTextBrush" as a brush.

    	Media::Brush^ brush= dynamic_cast<Media::Brush^>( App::Current->Resources->Lookup( "ApplicationTextBrush" ) );
    

    I tested it with both light and dark Requested Themes, and I saw the desired dark and light lines.

    • Marked as answer by Andrew7Webb Friday, May 25, 2012 6:59 PM
    Friday, May 25, 2012 6:41 PM
  • Glad that it works!

    Friday, May 25, 2012 6:47 PM