I created a simple .WPF app. No images, just components provided by Visual Studio. But, when I published it (single file, win-x64), it was 140 mb. Seems kinda crazy to me, considering that apps like yWriter are like 10mb big. So, what did I do wrong?
The app has multiple classes, buttons, events, DispatcherTimer, etc.
I just want to export the project into a single executable file of minimum size.Not sure if full code will help but here you go:
Code:
/// MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Beehive_Beesness
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Queen queen = new Queen();
private DispatcherTimer timer = new();
public MainWindow()
{
InitializeComponent();
statusReport.Text = queen.StatusReport;
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object? sender, EventArgs e)
{
WorkShift();
}
private void AssignJob_Click(object sender, RoutedEventArgs e)
{
queen.AssignBee(jobSelector.Text);
statusReport.Text = queen.StatusReport;
}
private void WorkShift()
{
if (HoneyVault.honey < queen.HoneyRequiredPerShift)
{
AssignJobButton.IsEnabled = false;
statusReport.Text = "Had to file for Bee-nkrupcy!";
return;
}
queen.WorkTheNextShift();
statusReport.Text = queen.StatusReport;
}
private void Start_Click(object sender, RoutedEventArgs e)
{
StartButton.IsEnabled = false;
timer.Interval = TimeSpan.FromSeconds(1.5);
timer.Start();
}
}
}
/// Bee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Beehive_Beesness
{
internal class Bee
{
public virtual float CostPerShift { get; }
public string Job { get; private set; }
public Bee(string job)
{
Job = job;
}
public void WorkTheNextShift()
{
if (HoneyVault.ConsumeHoney(CostPerShift))
{
DoJob();
}
}
protected virtual void DoJob() { }
}
class HoneyManufacturer : Bee
{
public const float NECTAR_PROCESSED_PER_SHIFT = 33.15f;
public override float CostPerShift { get { return 1.7f; } }
public HoneyManufacturer() : base("Honey Manufacturer")
{
}
protected override void DoJob()
{
HoneyVault.ConvertNectarToHoney(NECTAR_PROCESSED_PER_SHIFT);
}
}
class NectarCollector : Bee
{
public override float CostPerShift { get { return 1.95f; } }
public const float NECTAR_COLLECTED_PER_SHIFT = 33.25f;
public NectarCollector() : base("Nectar Collector")
{
}
protected override void DoJob()
{
HoneyVault.CollectNectar(NECTAR_COLLECTED_PER_SHIFT);
}
}
class EggCare : Bee
{
public const float CARE_PROGRESS_PER_SHIFT = 0.15f;
public override float CostPerShift { get { return 1.35f; } }
private Queen queen;
public EggCare(Queen queen) : base("Egg Care")
{
this.queen = queen;
}
protected override void DoJob()
{
queen.CareForEggs(CARE_PROGRESS_PER_SHIFT);
}
}
class Queen : Bee
{
public const float EGGS_PER_SHIFT = 0.45f;
public const float HONEY_PER_UNASSIGNED_WORKER = 0.5f;
public string StatusReport { get; private set; }
public float HoneyRequiredPerShift { get; private set; }
public override float CostPerShift { get { return 2.2f; } }
public Bee[] workers = new Bee[0];
private float eggs = 0;
private float unassignedWorkers = 5;
public Queen() : base("Queen")
{
AssignBee("Nectar Collector");
AssignBee("Honey Manufacturer");
AssignBee("Egg Care");
}
private void AddWorker(Bee worker)
{
if (unassignedWorkers >= 1)
{
unassignedWorkers--;
Array.Resize(ref workers, workers.Length + 1);
workers[workers.Length - 1] = worker;
}
}
public void AssignBee(string job)
{
switch (job)
{
case "Nectar Collector":
AddWorker(new NectarCollector());
break;
case "Honey Manufacturer":
AddWorker(new HoneyManufacturer());
break;
case "Egg Care":
AddWorker(new EggCare(this));
break;
}
UpdateStatusReport();
}
private string WorkerStatus(string job)
{
int count = 0;
string s = "s";
foreach(Bee bee in workers)
{
if (bee.Job == job) count++;
}
if (count == 1) s = "";
return $"{count} {job}{s}";
}
private void UpdateStatusReport()
{
float HoneyRequired = 0f;
int HoneyManufacturerCount = 0;
foreach (Bee bee in workers)
{
HoneyRequired += bee.CostPerShift;
if (bee.Job == "Honey Manufacturer") HoneyManufacturerCount++;
}
HoneyRequired += unassignedWorkers * 0.5f;
HoneyRequiredPerShift = HoneyRequired;
StatusReport = "HONEY VAULT REPORT\n"+HoneyVault.StatusReport
+ $"\n\nHoney Required Per Shift: {HoneyRequiredPerShift}\nHoney Created Per Shift*: {HoneyManufacturer.NECTAR_PROCESSED_PER_SHIFT * HoneyManufacturerCount * HoneyVault.NECTAR_CONVERSION_RATIO}\n\n*if nectar is available\n\nWORKER REPORT\nEgg Count: {eggs:0.0}\nUnassigned Workers: {unassignedWorkers: 0.0}\n{WorkerStatus("Honey Manufacturer")}\n{WorkerStatus("Nectar Collector")}\n{WorkerStatus("Egg Care")}\nTOTAL WORKERS: {workers.Length}";
}
protected override void DoJob()
{
// Add eggs
eggs += EGGS_PER_SHIFT;
// Tell each worker to do work
foreach (Bee bee in workers)
{
bee.WorkTheNextShift();
}
// Consume honey
HoneyVault.ConsumeHoney(HONEY_PER_UNASSIGNED_WORKER * workers.Length);
// Update Status Report
UpdateStatusReport();
}
public void CareForEggs(float eggsToConvert)
{
if (eggsToConvert > eggs) eggsToConvert = eggs;
eggs -= eggsToConvert;
unassignedWorkers += eggsToConvert;
}
}
}
/// HoneyVault.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Beehive_Beesness
{
internal static class HoneyVault
{
public const float NECTAR_CONVERSION_RATIO = .19f;
const float LOW_LEVEL_WARNING = 10f;
public static float honey = 25f;
private static float nectar = 100f;
public static void ConvertNectarToHoney(float amount)
{
if (amount <= nectar)
{
nectar -= amount;
}
else
{
amount = nectar;
nectar = 0f;
}
honey += amount*NECTAR_CONVERSION_RATIO;
}
public static bool ConsumeHoney(float amount)
{
if (honey > amount)
{
honey -= amount;
return true;
}
return false;
}
public static void CollectNectar(float amount)
{
if (amount > 0f) nectar += amount;
}
public static string StatusReport
{
get
{
string status = $"{honey:0.0} units of honey\n{nectar:0.0} units of nectar";
string warnings = "";
if (honey < LOW_LEVEL_WARNING)
{
warnings += "\nLOW HONEY LEVELS - ADD A HONEY MANUFACTURER!";
}
if (nectar < LOW_LEVEL_WARNING)
{
warnings += "\nLOW NECTAR LEVELS - ADD A NECTAR COLLECTOR!";
}
return status + warnings;
}
}
}
}
Used Visual Studio to export single file, win-x64, ready-to-run .NET Core 6 WPF Application