1

我正在尝试在 Canvas 上放置一个 Rectangle ,但无法弄清楚执行此操作的正确语法是什么。在 C# 中我会写

rect = new Rectangle();
        rect.Width = 100D;
        rect.Height = 50D;
        rect.Fill = new SolidColorBrush(Colors.Red);
        rect.StrokeThickness = 2;
        rect.Stroke = new SolidColorBrush(Colors.Black);

        Canvas.SetLeft(rect,5);
        Canvas.SetTop(rect,5);
        DrawCanvas.Children.Add(rect);

我尝试对嵌入式代码做同样的事情,但在定位矩形时遇到了麻烦。在嵌入式 Silverlight 中以编程方式在画布上创建和定位形状的正确方法是什么?这是我到目前为止所拥有的,但是当运行它时,Canvas 上什么也没有显示。

HRESULT MainPage::BtnOk_Click (IXRDependencyObject* pSender, XRMouseButtonEventArgs* ?  pArgs)
{
HRESULT hr = E_NOTIMPL;

if ((NULL == pSender) || (NULL == pArgs))
{
    hr = E_INVALIDARG;
}

IXRApplicationPtr pApplication;
if (FAILED(hr = App::GetApplication(&pApplication)))
    return hr;

IXRVisualHostPtr pVisualHost;
if (FAILED(hr = App::GetVisualHost(&pVisualHost)))
    return hr;

//Create Brush
IXRSolidColorBrushPtr pPaintBrushRed;
IXRSolidColorBrushPtr pPaintBrushBlack;

COLORREF brushColorRed = RGB(255,0,0);
COLORREF brushColorBlack = RGB(0,0,0);

pApplication->CreateObject(&pPaintBrushRed);
pPaintBrushRed->SetColor(brushColorRed);

pApplication->CreateObject(&pPaintBrushBlack);
pPaintBrushBlack->SetColor(brushColorBlack);


IXRRectanglePtr pRectangle;
IXRFrameworkElementPtr pRootElement;
IXRCanvasPtr pCanvasPanel;
IXRUIElementCollectionPtr pChildrenCollection;

pApplication->CreateObject(IID_IXRRectangle, &pRectangle);  
pRectangle->SetWidth(100);
pRectangle->SetHeight(50);
pRectangle->SetFill(pPaintBrushRed);
pRectangle->SetStroke(pPaintBrushBlack);
pRectangle->SetStrokeThickness(2);
pRectangle->SetName(TEXT("MyRect"));


pVisualHost->GetRootElement(&pRootElement); 
pRootElement->FindName(TEXT("DrawCanvas"), &pCanvasPanel);

pCanvasPanel->GetChildren(&pChildrenCollection);
pChildrenCollection->Add(pRectangle, NULL);
pCanvasPanel->SetAttachedProperty(L"MyRect.Left",NULL,5);
pCanvasPanel->SetAttachedProperty(L"MyRect.Top",NULL,5);



/*rect = new Rectangle();
        rect.Width = 100D;
        rect.Height = 50D;
        rect.Fill = new SolidColorBrush(Colors.Red);
        rect.StrokeThickness = 2;
        rect.Stroke = new SolidColorBrush(Colors.Black);

        Canvas.SetLeft(rect,5);
        Canvas.SetTop(rect,5);
        DrawCanvas.Children.Add(rect);*/
return hr;
}

XAML 代码:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ChartApplication"
x:Class="ChartApplication.MainPage"
Width="640" Height="480">

<Grid x:Name="LayoutRoot" Background="White">
    <local:ChartControl Margin="97,58,82,89"/>
    <Rectangle Fill="#FFE25E5E" Stroke="Black" Height="133" Margin="97,98,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="217"/>
    <Ellipse Fill="#FFB7E25E" Stroke="Black" Height="105" HorizontalAlignment="Right" Margin="0,0,41,78" VerticalAlignment="Bottom" Width="235"/>
    <Button x:Name="BtnOk" Height="85" HorizontalAlignment="Left" Margin="113,0,0,89" VerticalAlignment="Bottom" Width="155" Content="Ok" Click="BtnOk_Click"/>
    <Canvas x:Name="DrawCanvas" HorizontalAlignment="Right" Margin="0,98,41,198" Width="240" Background="#FFE4E4F2"/>

</Grid>

4

1 回答 1

0

画布可能不支持嵌入式 Silverlight。可以直接添加到 Grid

IXRUIElementCollectionPtr pElementCollection;
m_pLayoutRoot->GetChildren(&pElementCollection);
int nIndex=0;
pChildrenCollection->Add(pRectangle, &nIndex);
nIndex++;
于 2012-07-03T16:12:54.567 回答