1

캔버스에 사각형을 배치하려고하는데 올바른 구문이 무엇인지 알아낼 수 없습니다. C#에서는 쓸 것입니다Embedded Silverlight에서 사각형을 배치하는 방법은 무엇입니까?

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에서 프로그램 방식으로 캔버스의 모양을 만들고 배치하는 올바른 방법은 무엇입니까? 이것은 내가 지금까지 가지고있는 것이지만 그것을 실행할 때 캔버스에는 아무것도 나타나지 않는다.

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> 

답변

0

캔버스가 포함 된 실버 라이트에서 지원하지 않을 수 있습니다. 그리드에 직접 추가 할 수 있습니다.

IXRUIElementCollectionPtr pElementCollection; 
m_pLayoutRoot->GetChildren(&pElementCollection); 
int nIndex=0; 
pChildrenCollection->Add(pRectangle, &nIndex); 
nIndex++;