Witam, w aplikacji WPF, którą tworzę potrzebuje wyeksportować Canvasa do pliku XLS. Problem polega na tym, że metoda, której używam do eksportu działa tylko dla Canvasa utworzonego wcześniej w XAMLu, kiedy próbuję wyeksportować Canvasa tworzonego w kodzie nic się nie wyeksportowywuje. Canvasa tworzę w zdarzeniu Loaded dodaję go do Grida, który jest zdefiniowany w XAMLu, wywoływanie metody UpdateLayout() nic nie pomaga. Czy jest jakaś różnica między obiektem utworzonym w XAMLu a z poziomu kodu?
Podaj kod w C# i XAMLu
<Window x:Class="Application.Raport"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Raport karta monet" Height="1235" Width="873" Background="Gray">
<ScrollViewer>
<Canvas Name="ExportCanvas" Width="794" Height="1123" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
<Canvas.Effect>
<DropShadowEffect />
</Canvas.Effect>
<TextBlock Canvas.Left="78" Canvas.Top="78" Height="51" Text="Raport
" FontFamily="Arial" FontSize="36" FontWeight="Bold" />
<Rectangle Canvas.Left="78" Canvas.Top="125" Height="2" Fill="Black" Width="636" />
<TextBlock Canvas.Left="78" Canvas.Top="135" Height="29" Text="Dla wszystkich wyświetlanych rekordów" FontFamily="Arial" FontSize="18" FontWeight="ExtraLight" />
<Canvas Width="556" Height="223" Background="White" Canvas.Left="119" Canvas.Top="200">
<TextBlock Height="23" Text="Wartość w ostatnich miesiącach:" FontSize="16" />
<Canvas Width="556" Height="150" Margin="0,23,0,0" Background="Transparent" Name="DiagramMiesiace"/>
<Canvas Width="556" Height="50" Margin="0,173,0,0" Background="Transparent" Name="PodpisyMiesiace"/>
</Canvas>
<Canvas Width="556" Height="223" Background="White" Canvas.Left="119" Canvas.Top="450">
<TextBlock Height="23" Text="Wartość w ostatnich kwartałach:" FontSize="16" />
<Canvas Width="556" Height="150" Margin="0,23,0,0" Background="Transparent" Name="DiagramKwartaly"/>
<Canvas Width="556" Height="50" Margin="0,173,0,0" Background="Transparent" Name="PodpisyKwartaly"/>
</Canvas>
<Canvas Width="556" Height="223" Background="White" Canvas.Left="119" Canvas.Top="700">
<TextBlock Height="23" Text="Wartość w ostatnich latach:" FontSize="16" />
<Canvas Width="556" Height="150" Margin="0,23,0,0" Background="Transparent" Name="DiagramLata"/>
<Canvas Width="556" Height="50" Margin="0,173,0,0" Background="Transparent" Name="PodpisyLata"/>
</Canvas>
</Canvas>
</ScrollViewer>
</Window>
public partial class Raport : Window
{
public Raport()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Raport_Loaded);
}
void Raport_Loaded(object sender, RoutedEventArgs e)
{
CreateChart(DiagramMiesiace, PodpisyMiesiace, Colors.Blue, new ChartArgument("01.2001", 10), new ChartArgument("02.2001", 35), new ChartArgument("03.2001", -20), new ChartArgument("04.2001", -34), new ChartArgument("05.2001", 50));
CreateChart(DiagramKwartaly, PodpisyKwartaly, Colors.Red, new ChartArgument("I-2001", -34), new ChartArgument("II-2001", -500), new ChartArgument("III-2001", -20), new ChartArgument("IV-2001", -34), new ChartArgument("V-2001", -34), new ChartArgument("VI-2001", 200), new ChartArgument("I-2001", -34), new ChartArgument("II-2001", -500));
CreateChart(DiagramLata, PodpisyLata, Colors.Green, new ChartArgument("1997", -34), new ChartArgument("1998", 1000), new ChartArgument("1999", 1500), new ChartArgument("2000", 2350), new ChartArgument("2001", 3450), new ChartArgument("2002", 200), new ChartArgument("2003", -34));
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
saveDialog.OverwritePrompt = true;
saveDialog.FileName = "Raport";
saveDialog.DefaultExt = "xps";
saveDialog.Filter = "Raport (*.XPS)|*.xps";
if ((bool)saveDialog.ShowDialog())
{
Export(saveDialog.FileName, ExportCanvas);
}
}
private void CreateChart(Canvas diagram, Canvas podpisy, Color lineColor, params ChartArgument[] values)
{
double fontSize = 10;
Transform transform1 = diagram.LayoutTransform;
// Temporarily reset the layout transform before saving
diagram.LayoutTransform = null;
// Get the size of the canvas
Size size1 = new Size(diagram.Width, diagram.Height);
// Measure and arrange elements
diagram.Measure(size1);
diagram.Arrange(new Rect(size1));
Transform transform2 = podpisy.LayoutTransform;
// Temporarily reset the layout transform before saving
podpisy.LayoutTransform = null;
// Get the size of the canvas
Size size2 = new Size(podpisy.Width, podpisy.Height);
// Measure and arrange elements
podpisy.Measure(size2);
podpisy.Arrange(new Rect(size2));
List<double> lst = new List<double>();
for (int i = 0; i < values.Length; i++)
{
lst.Add(values[i].VALUE);
}
double maxValue = lst.Max();
double minValue = lst.Min();
double itemSpacing = diagram.Width / (values.Length - 1);
double unit = 0;
if (minValue > 0)
{
unit = diagram.Height / (maxValue - minValue);
}
if (minValue < 0)
{
unit = diagram.Height / (maxValue + Math.Abs(minValue));
}
for (int i = 0; i < values.Length - 1; i++)
{
if (minValue > 0)
{
Line ln = new Line();
ln.Stroke = new SolidColorBrush(lineColor);
ln.StrokeThickness = 3;
ln.X1 = itemSpacing * i;
ln.Y1 = diagram.Height - (values[i].VALUE * unit);
ln.X2 = itemSpacing * (i + 1);
ln.Y2 = diagram.Height - (values[i + 1].VALUE * unit);
diagram.Children.Add(ln);
}
if (minValue < 0)
{
Line ln = new Line();
ln.Stroke = new SolidColorBrush(lineColor);
ln.StrokeThickness = 3;
ln.X1 = itemSpacing * i;
ln.Y1 = diagram.Height - (values[i].VALUE * unit) + (minValue * unit);
ln.X2 = itemSpacing * (i + 1);
ln.Y2 = diagram.Height - (values[i + 1].VALUE * unit) + (minValue * unit);
diagram.Children.Add(ln);
}
}
for (int i = 0; i < values.Length; i++)
{
TextBlock plbl = new TextBlock();
plbl.Text = values[i].PERIOD.ToString();
plbl.FontSize = fontSize;
plbl.Width = 50;
double toMovp = (plbl.Width / 2) + plbl.Width / values.Length;
Canvas.SetTop(plbl, 0);
Canvas.SetLeft(plbl, i*itemSpacing);
podpisy.Children.Add(plbl);
TextBlock tlbl = new TextBlock();
tlbl.Text = values[i].VALUE.ToString();
tlbl.FontSize = fontSize;
tlbl.Width = 50;
double toMovt = (plbl.Width / 2) + plbl.Width / values.Length;
Canvas.SetBottom(tlbl, 0);
Canvas.SetLeft(tlbl, i* itemSpacing);
podpisy.Children.Add(tlbl);
}
}
public void Export(string path, FrameworkElement surface)
{
if (path == null) return;
// Save current canvas transorm
Transform transform = surface.LayoutTransform;
// Temporarily reset the layout transform before saving
surface.LayoutTransform = null;
// Get the size of the canvas
Size size = new Size(surface.Width, surface.Height);
// Measure and arrange elements
surface.Measure(size);
surface.Arrange(new Rect(size));
// Open new package
Package package = Package.Open(path, FileMode.Create);
// Create new xps document based on the package opened
XpsDocument doc = new XpsDocument(package);
// Create an instance of XpsDocumentWriter for the document
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
// Write the canvas (as Visual) to the document
writer.Write(surface);
// Close document
doc.Close();
// Close package
package.Close();
// Restore previously saved layout
surface.LayoutTransform = transform;
}
}
To jest działający kod. Kiedy tworzę Canvasy przekazywane do metody CreateChart w metodzie Loaded, kod przestaje działać.